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 execut开发者_运维问答ing 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
if you don't want to "inspect" the stack, you could use a decorator on your method to store it in your dictionnary and avoid to launch it twice.
function_list = []
def method_singleton(function):
global function_list
def decorated_method(*args, **kwargs):
if function.__name__ not in function_list:
function_list.append(function.__name__)
return function(*args, **kwargs)
else:
print "Method %s already called"%function.__name__
return decorated_method
@method_singleton
def method_to_decorate(arg1, arg2):
pass
where "function_list" is the list of functions already called (i don't know how you manage your dictionnary)
Perhaps you should decorate each function that you're calling with an onlyonce
decorator? That would be more pythonic. A proof of concept follows.
called = set()
def onlyonce(fn):
def decorated(*largs, **kargs):
if fn not in called:
called.add(fn)
print "Calling"
fn(*largs, **kargs)
else:
print "Already called"
return decorated
@onlyonce
def test_function():
print "I am getting called now"
test_function()
test_function()
test_function()
test_function()
Also, functions are "immutable" and can be stored as dictionary keys. You don't have to rely on the names. This might be an advantage (or a disadvantage) depending on your use.
精彩评论