Skip to content Skip to sidebar Skip to footer

Is There A Generic Way For A Function To Reference Itself?

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(

Solution 1:

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:

from functools import wraps
defshowinfo(f):
    @wraps(f)defwrapper(*args, **kwds):
         print(f.__name__, f.__hash__)
         return f(*args, **kwds)
    return wrapper

@showinfodefaa():
    pass

If you really do need to reference the function, then just add it to the function arguments:

defwithself(f):
    @wraps(f)defwrapper(*args, **kwds):
        return f(f, *args, **kwds)
    return wrapper

@withselfdefaa(self):
      print(self.__name__)
      # etc.

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:

defbind(f):
    """Decorate function `f` to pass a reference to the function
    as the first argument"""return f.__get__(f, type(f))

@binddeffoo(self, x):
    "This is a bound function!"print(self, x)


>>> foo(42)
<function foo at 0x02A46030> 42>>> help(foo)
Help on method foo in module __main__:

foo(self, x) method of builtins.function instance
    This is a bound function!

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.

Solution 2:

http://docs.python.org/library/inspect.html looks promising:

import inspect
deffoo():
     felf = globals()[inspect.getframeinfo(inspect.currentframe()).function]
     print felf.__name__, felf.__doc__

you can also use the sys module to get the name of the current function:

import sys
defbar():
     felf = globals()[sys._getframe().f_code.co_name]
     print felf.__name__, felf.__doc__

Solution 3:

You can at least say self = bb in the first line, and then you only need to change that line when you change the function name, instead of every other reference.

My code editor highlights the variable self the same way it does for classes, too.

Solution 4:

How about a quick hack to make your own "self" name, like this:

>>>deff():...    self = f...print"My name is ", self.__name__, "and I am", self.__hash__...>>>f()
My name is  f and I am <method-wrapper '__hash__' of function object at 0x00B50F30>
>>>x = f>>>x()
My name is  f and I am <method-wrapper '__hash__' of function object at 0x00B50F30>
>>>

Post a Comment for "Is There A Generic Way For A Function To Reference Itself?"