Skip to content

Instantly share code, notes, and snippets.

@lukmdo
Last active February 2, 2018 22:09
Show Gist options
  • Save lukmdo/7cdad7b657afb362aa5530b4d58d6f1e to your computer and use it in GitHub Desktop.
Save lukmdo/7cdad7b657afb362aa5530b4d58d6f1e to your computer and use it in GitHub Desktop.
get caller func name
from __future__ import print_function
import inspect
def fn():
return inspect.stack()[1][3]
class Example(object):
@classmethod
def get_classmethod_call(cls):
print(fn(), cls)
def get_method_call(self):
print(fn(), self)
def get(self):
print(fn())
self.get_method_call()
self.get_classmethod_call()
if __name__ == '__main__':
# standard
Example.get_classmethod_call()
# lesser standard but still valid
Example().get_classmethod_call()
# standard
Example().get_method_call()
# calls both classmethod and method
Example().get()
# invalid
# Example.get_method_call()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment