function pointers

Erik Max Francis max at alcyone.com
Thu Apr 26 11:18:10 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?

On the contrary.  Functions and methods are first class types, so you
can move them around, store them, and call them as much as you like:

>>> def f(x):
...  print x
... 
>>> f
<function f at 0x81950bc>
>>> g = f
>>> g
<function f at 0x81950bc>
>>> f(1)
1
>>> g(1)
1

It even has both bound and unbound methods, something which, for
instance, C++ does not have:

>>> class C:
...  def m(self, x):
...   print x
... 
>>> c = C()
>>> unbound = C.m
>>> unbound(c, 1) # must call with `self' argument explicitly
1
>>> bound = c.m
>>> bound(1) # `self' argument implied, not needed
1


-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ All the gods are dead except the god of war.
\__/ Leroy Eldridge Cleaver
    The laws list / http://www.alcyone.com/max/physics/laws/
 Laws, rules, principles, effects, paradoxes, etc. in physics.



More information about the Python-list mailing list