Function pointers in Python?
Fredrik Lundh
effbot at telia.com
Tue Sep 12 16:41:54 EDT 2000
richard harvey wrote:
> I know there is no direct correlation to C function pointers in Python, but
> I'm curious if I can pull of a similar effect. I'm wanting to define a
> dictionary that pairs a value with a Python function. If I'm provided the
> value, I would find the matching dictionary entry and then call the Python
> function that was paired with the value. If there is a way to do this, how
> would I assign the function name into the dictionary, and how should I
> retrieve the function and call it from the dictionary?
why didn't you just try it ;-)
def myfunc(x):
print x
dict = {
"myfunc": myfunc
}
dict["myfunc"]("hello")
func = dict.get("myfunc")
if callable(func):
func(10)
etc. you can use functions, bound methods, class constructors,
and lambdas in the same way (they're all callable).
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list