[Tutor] Accessing the name of a Function

Andreas Kostyrka andreas at kostyrka.org
Wed Dec 13 01:04:42 CET 2006


* Carroll, Barry <Barry.Carroll at psc.com> [061212 23:54]:
>    Greetings:
> 
> 
> 
>    Is it possible, from inside a stand-alone function (not a member of a
>    class), to access the string representation of the function's name?  If
>    so, how?
Probably, but it's dirty like help (sys._getframe would be your ticket
into this), but you ask on the tutor mailing list, so you'll probably
don't want this:

def func(x):
     return 2 * x
 
gunc = func
del func

print gunc(10)

The best you can hope to derive is "func" here, but as you can see,
functions are only objects that can be assigned freely.

(Spoiler: it's sys._getframe(0).f_code.co_name, but it will always
only know the name the function was defined under. No way to know how
it was named when it was called. So it's not worth much.)

Andreas
>
> 
> 
>    Regards,
> 
> 
> 
>    Barry
> 
>    [1]barry.carroll at psc.com
> 
>    541-302-1107
> 
>    ________________________
> 
>    We who cut mere stones must always be envisioning cathedrals.
> 
>    --Quarry worker's creed
> 
> 
> 
> References
> 
>    Visible links
>    1. mailto:barry.carroll at psc.com

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list