Finding the name of a function while defining it

Mitya Sirenef msirenef at lightbird.net
Thu Dec 27 04:07:18 EST 2012


On 12/27/2012 03:26 AM, Abhas Bhattacharya wrote:
> On Thursday, 27 December 2012 13:33:34 UTC+5:30, Mitya Sirenef  wrote:
>> How about defining a function that prints value and then calls a function?
>>
>>
>>
>> def call(func_name):
>>
>>     print(mydict[func_name])
>>
>>     globals()[func_name]()
>>
>>
>>
>>
>>
>> You could also define a custom class that does the same thing on attribute
>>
>> lookup and do something like Call.func_name() .
>>
>>
>>
>>    -m
>>
>>
>>
>> -- 
>>
>> Lark's Tongue Guide to Python: http://lightbird.net/larks/
> Can you explain me what this means?
> globals()[func_name]()

globals() is a globals dictionary that maps function
names to function objects (along with other things),
so we get the function object by name and then
run it.

  -m

-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/




More information about the Python-list mailing list