function pointers
Fredrik Lundh
fredrik at pythonware.com
Thu Apr 26 08:05:52 EDT 2001
Brandon J. Van Every wrote:
> I get the feeling that Python doesn't have anything resembling a function
> pointer? i.e. no way to call a specific function according to the value a
> variable is set to? In fact, I get the feeling it doesn't have pointers of
> any sort at all?
everything is an object, and all variables are references to
objects.
the following statement creates an integer object, and binds
the name "bacon" to it:
bacon = 10
functions are no different from other objects:
def spam(egg):
print egg
creates a function object, and binds the name "spam" to it:
>>> spam
<function spam at 866e10>
you can treat function objects as any other object (pass it to
a function, return it from a function, append it to a list, etc).
to call it, just give the arguments in parentheses as usual:
def do(callback):
return callback("hello")
>>> do(spam)
hello
>>> do(len)
5
>>> do(list)
['h', 'e', 'l', 'l', 'o']
Cheers /F
More information about the Python-list
mailing list