late bindings ???

Gonçalo Rodrigues op73418 at mail.telepac.pt
Mon Dec 2 09:40:10 EST 2002


On 02 Dec 2002 21:22:38 +0800, "Alfredo P. Ricafort"
<alpot at mylinuxsite.com> wrote:

>Hi,
>
>I'm trying to create a flexible python program where the called
>functions are stored in a list.  But before you can assign the function
>name in a list, it must be in the namespace. However, in my case, the
>function names are known later. So what I did was to store it as a
>string instead and do something like this:
>
>func=['do_file','do_exit']
>apply(func[i],[args])

What did you expect this to do? You get a string (func[i]) and then try
to apply it to some args and... you get an exception.

Functions (as pretty much everything else) are first-class objects in
Python. This means that:

 - They can be assigned to variables
 - They can be passed as arguments to other functions
 - They can be returned as function arguments

>
>But this doesn't work!

I do not understand exactly what you want, but maybe the next snippet
will help you

>>> def test():
... 	print "A test!"
... 	
>>> #A dictionary of functions.
>>> funcdict = {}
>>> funcdict['myfunc'] = test
>>> funcdict['myfunc']()
A test!

HTH,
G. Rodrigues



More information about the Python-list mailing list