Function pointers in Python?

Warren Focke wfocke at phoenixdsl.com
Tue Sep 12 23:08:16 CEST 2000


Rich asked:
> 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.
[snip]
> 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?

You're thinking too hard:

>>> dict={}
>>> def spam(x):
...     return x**2
... 
>>> dict['eggs']=spam
>>> dict
{'eggs': <function spam at 80caf08>}
>>> dict['eggs'](3)
9

Warren Focke

-- 
Just because romance and rapture have so often served as a pretext for
curdled banality doesn't make the sentiments themselves obsolete.
 -- Michelle Goldberg



More information about the Python-list mailing list