I can access a python function’s attribute inside of function itself by below code:
def aa():
print aa.__name__
print aa.__hash__
# other simliar
However, if above aa() function is a template for write other code, say bb(), I have to write:
def bb():
print bb.__name__
print bb.__hash__
# other simliar
Is there a “pointer” similar to the self argument in a class method so I could write code like this?
def whatever():
print self.__name__
print self.__hash__
# other simliar
I searched and found someone said to use the class to solve this problem, but that may be a trouble to redefine all the existing functions. Any suggestions?
There is no generic way for a function to refer to itself. Consider using a decorator instead. If all you want as you indicated was to print information about the function that can be done easily with a decorator:
If you really do need to reference the function, then just add it to the function arguments:
Edit to add alternate decorator:
You can also write a simpler (and probably faster) decorator that will make the wrapped function work correctly with Python’s introspection:
This leverages Python’s descriptor protocol: functions have a
__get__method that is used to create bound methods. The decorator simply uses the existing method to make the function a bound method of itself. It will only work for standalone functions, if you wanted a method to be able to reference itself you would have to do something more like the original solution.