I want to know how to access a function from within it without naming it directly.
def fact(n):
this = # some code, where we don't use the name "fact"
print(fact == this) # True
if n < 1:
return 1
return n * this(n-1)
The code above should behave exactly like:
def fact(n):
if n < 1:
return 1
return n * fact(n-1)
Does anyone have any ideas?
EDIT:
My question is different from Determine function name from within that function (without using traceback). I need fact == this
in the code above to return True
.
EDIT2:
@ksbg's answer is a good one, but consider the folowing code:
from inspect import stack, currentframe
def a():
def a():
f_name = stack()[0][3] # Look up the function name as a string
this = currentframe().f_back.f_globals[f_name]
print(a == this)
a()
a() # False
In this case it wont work as expected.