def <dynamic function name> () syntax ?

Ype Kingma ykingma at accessforall.nl
Wed Apr 4 14:55:23 EDT 2001


Bruce Edge wrote:
> 
> Can the follwoing be accomplished in Python?
> 
> I want to create a func named "abc":
> 
> >>> name="abc"
> 
> >>> eval ("name")
> 'abc'
> 
> >>> def eval ("name") ():
>   File "<stdin>", line 1
>     def eval ("name") ():
>                    ^
> SyntaxError: invalid syntax

def yourfunction():
    return yourvalue

eval(name + '= yourfunction')

The interpreter uses dictionaries, so you might
just as well do that yourself:

yourfunctiondict['name'] = yourfunction

yourfunctiondict['name'](yourarg)


Good luck,
Ype



More information about the Python-list mailing list