Is there a generic way for a function to reference itself?

后端 未结 4 936
粉色の甜心
粉色の甜心 2020-12-03 00:50

I can access a python function\'s attribute inside of function itself by below code:

def aa():
    print aa.__name__
    print aa.__hash__
    # other simlia         


        
4条回答
  •  渐次进展
    2020-12-03 01:46

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

    import inspect
    def foo():
         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
    def bar():
         felf = globals()[sys._getframe().f_code.co_name]
         print felf.__name__, felf.__doc__
    

提交回复
热议问题