Skip to content

Instantly share code, notes, and snippets.

@GrahamDumpleton
Created August 25, 2015 00:37
Show Gist options
  • Save GrahamDumpleton/01d962cb085c28184819 to your computer and use it in GitHub Desktop.
Save GrahamDumpleton/01d962cb085c28184819 to your computer and use it in GitHub Desktop.
Qualified name of function being executed.
# Can't get class via stack frame as only have access to code object and not what type it may have been bound to if a function.
>>> def func():
... print sys._getframe().f_code.co_name
...
>>> func()
func
>>> class A(object):
... def method(self):
... print sys._getframe().f_code.co_name
...
>>> A().method()
method
# Even if use profile hook can't do it, as all one can get is stack frame again.
>>> def hook(*args):
... print args
...
>>> sys.setprofile(hook)
(<frame object at 0x101a56dd0>, 'return', None)
>>> A().method()
(<frame object at 0x101ba8050>, 'call', None)
(<frame object at 0x101ba8050>, 'c_call', <built-in function utf_8_decode>)
(<frame object at 0x101ba8050>, 'c_return', <built-in function utf_8_decode>)
(<frame object at 0x101ba8050>, 'return', (u'A().method()\n', 13))
(<frame object at 0x101b9a1f8>, 'call', None)
(<frame object at 0x101a56c20>, 'call', None)
(<frame object at 0x101a56c20>, 'c_call', <built-in function _getframe>)
(<frame object at 0x101a56c20>, 'c_return', <built-in function _getframe>)
method
(<frame object at 0x101a56c20>, 'return', None)
(<frame object at 0x101b9a1f8>, 'return', None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment