In Python, any function is an object. But how then to receive the link to this object in function? No self function inside does not see.

    2 answers 2

    And what prevents to use the name of the function?

     >>> def foo(): ... return foo ... >>> foo() <function foo at 0x7f7cd66fba60> >>> foo <function foo at 0x7f7cd66fba60> 

    If the function name is unknown, then you cannot get it. Similar functionality has been rejected .

    But as they say, if you really want, then there are several options. I would not recommend doing so without much need.

    Through inspect :

     import inspect def foo(): print inspect.stack()[0][3] 

    Through sys :

     import sys def foo(): print sys._getframe().f_code.co_name 

    You can get the method, knowing the name of the function, through the function globals()

    Upd

    Attention! According to the documentation, not all python implementations can include sys._getframe . This is a function for internal use of the CPython interpreter and may not work for others. Use at your own risk

    • And if I want to generalize and do not know the name of the function? - faoxis
    • one
      @faoxis in any way. Offered , but rejected - FeroxTL
    • 2
      @faoxis generally has a crutch to get the name of the form sys._getframe (). f_code.co_name, but I would not recommend it - FeroxTL

    You can try this:

     import inspect def foo(): name = inspect.stack()[0][3] print(name) print(globals()[name]) foo()