This answer shows that there is a built-in __name__
attribute of a function which can be used from outside the function, i.e. print f.__name__
. However, how can I get this attribute from within the function itself?
Just using unqualified __name__
does not help: print __name__
prints __main__
.
Using print f.__name__
from within f()
looks stupid - I can type "f"
just as well.
Alternatively, is there a kind of self
object for functions, i.e. can I get a pointer to the function that is executing in a common way?
I don't like the method proposed in this question - it feels that hacking the stack for such simple task is not the proper way.
Motivation: I have a dictionary of {keyword:function}
, the keywords are read from the input and an appropriate function is executed. I want each function to be executed only once, so I want each function to register itself in some data structure when executed. I know I can do it in the dictionary itself, but I thought of using a separate data structure for this.
Python version is 2.6.4 BTW