Function reference -> name?

Duncan Booth duncan at
Fri Nov 14 15:43:25 CET 2003

Rene Pijlman < at> wrote in 
news:2lo9rvsob1ejjju20ut308temf0p8l0gbo at

> When I have a reference to a function (as in parameter 'function' of f1
> below), how can I get the name of that function ('f2' in the example
> below)?
> def f1(function):
>     pass
>     # How to get 'f2' from function?
    	print function.__name__
> def f2():
>     pass
> f1(f2)
This will give you the name under which the function was originally 
defined, which of course may not be the name of the variable at the time 
you passed it in. Also if it was a lambda function you get the name 
'<lambda>', and if it is some other sort of callable object it may or may 
not have a __name__ attribute.

Duncan Booth                                             duncan at
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list