Skip to content

Instantly share code, notes, and snippets.

@me2beats
Last active September 16, 2019 19:41
Show Gist options
  • Save me2beats/8c491eda8c53447075ca194761451a93 to your computer and use it in GitHub Desktop.
Save me2beats/8c491eda8c53447075ca194761451a93 to your computer and use it in GitHub Desktop.
Called_from_here behavior
from inspect import currentframe, getouterframes
class CalledFromHereBhv:
def __init__(self):
curframe = currentframe()
self.called_from_module_fn,\
self.called_from_start_line =\
self._get_module_and_start_line(curframe)
def _get_module_and_start_line(self, curframe):
calframe = getouterframes(curframe, 2)
module, start_line = calframe[-1][1:3]
return (module, start_line)
from test import Test
Test()
from called_from_here_bhv import CalledFromHereBhv
class Test(CalledFromHereBhv):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print (self.called_from_module_fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment