top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is there a way to create a callback in Python?

+2 votes
728 views

Also is there a way to detect if the user presses a key in Python that works on most OS's? I've only seen 1 method and that only works in Python 2.6 and less. If you get the key, can you store it in a variable?

posted Sep 9, 2013 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

What is usually meant by "a callback" is a function object. In Python, functions are first class objects. You just use the function name without the parentheses.

def my_function():
 print "Executing my_function"

b = my_function # b is now a function object

b() 

Likewise, instead of storing it in a global, you might pass it to a method which stores it as an object attribute, or whatever.

Also of interest is that you can easily create partial functions, where some of the parameters are already decided. See the docs for functools.partial

And if you're trying to use a method as a callback, you can store the bound-method, which is effectively a partial including the self parameter.

Finally, don't forget lambda functions, which can be useful if you're trying to create a simple function and don't need a name for it.

answer Sep 9, 2013 by Amit Parthsarthi
+1 vote

Your question is somewhat vague.
Are you asking for a function that waits for the user to press a key and then returns the key that was pressed? Or are you asking for a function that detects whether a key has been pressed at all?

answer Sep 9, 2013 by anonymous
Similar Questions
+2 votes

Is there a way to use pdb to debug Google apps written in Python? When I start the development system to run the app "test" like this -

'./google_appengine/dev_appserver.py ./test'

  • I'd like to send the program into debug. I couldn't see anything in the documentation how to do this. If I do this - python -mpdb './google_appengine/dev_appserver.py' './test'
  • then dev_appserver.py goes into debug but doesn't know anything about "test".
+1 vote

I'd like to install ALL Python packages on my machine. Even if it takes up 4-5GB, or more, I'd like to get everything, and then use it when I need it. Now, I'd like to import packages, like numpy and pandas, but nothing will install. I figure, if I can just install everything, I can simply use it when I need it, and if I don't need it, then I just won't use it.

I know R offers this as an option. I figure Python must allow it too.

Any idea how to grab everything?

+2 votes

Is it possible to catch exceptions raised in callbacks like after_create in the controller?

0 votes

I can create a list that has repeated elements of another list as follows:

xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
 for i in range(nrep):
 yy.append(aa)
print yy

output:

['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']

Is there a one-liner to create a list with repeated elements?

+3 votes

Is there way to get list of instances of particular class through class itself? via metaclass or any other method?

If class is object is it possible to delete it? If it is possible then how instances of that class will behave?

...