Last active
February 2, 2018 22:09
-
-
Save lukmdo/7cdad7b657afb362aa5530b4d58d6f1e to your computer and use it in GitHub Desktop.
get caller func name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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