function pointer

laotseu bdesth at nospam.free.fr
Thu Sep 12 06:08:45 CEST 2002


Martin Franklin wrote:
> On Wednesday 11 Sep 2002 10:15 am, Christina wrote:
> 
>>Hi!
>>
>>Does Python have function pointers? If not, is there a possibility to
>>emulate them?
> 
> 
> 
> Excuse my ignorance but what is a function pointer....  
> could this be the answer:-
> 
> 
> def aFunction():
>     print "aFunction was called"
> 
> ## call it
> aFunction()
> 
> ## create a reference to it
> af=aFunction
> 
> ## call the reference
> af()
> 
> ## at the python command line:-
> 
> [dev]$ python
> Python 2.2.1 (#3, May 29 2002, 20:32:44)
> [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>def aFunction():
>>>
> ....     print "aFunction was called"
> ....
> 
>>>>aFunction()
>>>
> aFunction was called
> 
>>>>af=aFunction
>>>>af()
>>>
> aFunction was called
> 
> 
> 
> Regards
> Martin
> 
> 

Well, it's not exactly a function pointer since there is no pointers in 
Python, but yes, the idea is the same.

Function pointers are often used in C to allow 'generic' behavior (like 
with the qsort() function) or as 'callback' functions to handle user 
events in GUI programming (see the Gtk+ toolkit).

Laotseu




More information about the Python-list mailing list