Skip to content

Instantly share code, notes, and snippets.

@muzhig
Last active January 1, 2016 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzhig/8086673 to your computer and use it in GitHub Desktop.
Save muzhig/8086673 to your computer and use it in GitHub Desktop.
python class name inside it's declaration
def get_classname():
return traceback.extract_stack()[-2][2] # in inner call
def get_classname_for_method_decorator():
return traceback.extract_stack()[-3][2] # in inner call
def decorator(func):
classname = get_classname_for_method_decorator()
return func
class Foo(object):
class_name = traceback.extract_stack()[-1][2] # in class definition
class_name = get_classname()
@decorator
def foo_method(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment