Getting the Python function for a code object

Through the inspect.getframeinfo module. I mean -- there is no straightforward way of doing that in Python -- Most times you can get hold of the code object, without having the function already, it is through frame instrospection.

Inspect's getframeinfo function does return some information about the frame being run, then you can retrieve the function object by getting its name.

Tough this is implementation dependent and have some drawbacks:

>>> import inspect
>>> def a():
...   return inspect.currentframe()
... 

>>> inspect.getframeinfo(a())
Traceback(filename='<stdin>', lineno=2, function='a', code_context=None, index=None)
>>> b = inspect.getframeinfo(a())
>>> b.function
'a'

Another way, but still implementation dependent, is to use the gc module (garbage collector) to get referrers to said code object.

>>> import gc
>>> from types import FunctionType
>>> def a(): pass
... 
>>> code = a.__code__

>>> [obj for  obj in  gc.get_referrers(code) if isinstance(obj, FunctionType)  ][0]
<function a at 0x7f1ef4484500>
>>> 

-- This is for Python 3 - for Python 2 one should replace __code__ by func_code