Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

is b at the end
Source Link
AXO
  • 9.2k
  • 6
  • 73
  • 69
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # first_arg_name in this case is 'self', but could be sth else
 first_arg_name = caller_frame.f_code.co_varnames[0]
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals[first_arg_name]
 assert caller_instance is objcaller_frame.f_back.f_locals['b'] is b
 assert type(caller_instance).__name__ == 'B'
 assert function == caller_frame.f_code.co_name == 'class_B_fun'
 assert filename == caller_frame.f_code.co_filename == __file__
 assert getmodulename(filename) == Path(__file__).stem
class B(object):
 def class_B_fun(self):
 obja = A()
 obja.class_A_fun()
if __name__ == "__main__":
 objb = B()
 objb.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # first_arg_name in this case is 'self', but could be sth else
 first_arg_name = caller_frame.f_code.co_varnames[0]
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals[first_arg_name]
 assert caller_instance is obj
 assert type(caller_instance).__name__ == 'B'
 assert function == caller_frame.f_code.co_name == 'class_B_fun'
 assert filename == caller_frame.f_code.co_filename == __file__
 assert getmodulename(filename) == Path(__file__).stem
class B(object):
 def class_B_fun(self):
 obj = A()
 obj.class_A_fun()
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # first_arg_name in this case is 'self', but could be sth else
 first_arg_name = caller_frame.f_code.co_varnames[0]
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals[first_arg_name]
 assert caller_instance is caller_frame.f_back.f_locals['b'] is b
 assert type(caller_instance).__name__ == 'B'
 assert function == caller_frame.f_code.co_name == 'class_B_fun'
 assert filename == caller_frame.f_code.co_filename == __file__
 assert getmodulename(filename) == Path(__file__).stem
class B(object):
 def class_B_fun(self):
 a = A()
 a.class_A_fun()
if __name__ == "__main__":
 b = B()
 b.class_B_fun()
update for general first argument name
Source Link
AXO
  • 9.2k
  • 6
  • 73
  • 69
from inspect import currentframe, getframeinfo, getmodulename
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals['self']
 print(f'caller instance: {caller_instance}') # → obj
 print(f'caller from class: {type(caller_instance).__name__}') # → B
 print(f'caller from method: {function}') # → class_B_fun
 # alternatively:
 code = caller_frame.f_code
 assert function == code.co_name
 print(f'caller object file name or path: {filename}') # → 'foomodule.py'
 # alternatively:
 assert filename == code.co_filename
 module_name = getmodulename(filename)
 print(f'caller module: {module_name}') # → foomodule
class B(object):
 def class_B_fun(self): 
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # first_arg_name in this case is 'self', but could be sth else
 first_arg_name = caller_frame.f_code.co_varnames[0]
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals[first_arg_name]
 assert caller_instance is obj
 assert type(caller_instance).__name__ == 'B'
 assert function == caller_frame.f_code.co_name == 'class_B_fun'
 assert filename == caller_frame.f_code.co_filename == __file__
 assert getmodulename(filename) == Path(__file__).stem
class B(object):
 def class_B_fun(self):
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals['self']
 print(f'caller instance: {caller_instance}') # → obj
 print(f'caller from class: {type(caller_instance).__name__}') # → B
 print(f'caller from method: {function}') # → class_B_fun
 # alternatively:
 code = caller_frame.f_code
 assert function == code.co_name
 print(f'caller object file name or path: {filename}') # → 'foomodule.py'
 # alternatively:
 assert filename == code.co_filename
 module_name = getmodulename(filename)
 print(f'caller module: {module_name}') # → foomodule
class B(object):
 def class_B_fun(self): 
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
from pathlib import Path
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # first_arg_name in this case is 'self', but could be sth else
 first_arg_name = caller_frame.f_code.co_varnames[0]
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals[first_arg_name]
 assert caller_instance is obj
 assert type(caller_instance).__name__ == 'B'
 assert function == caller_frame.f_code.co_name == 'class_B_fun'
 assert filename == caller_frame.f_code.co_filename == __file__
 assert getmodulename(filename) == Path(__file__).stem
class B(object):
 def class_B_fun(self):
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
stack was not used
Source Link
AXO
  • 9.2k
  • 6
  • 73
  • 69
from inspect import currentframe, getframeinfo, getmodulename, stack
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals['self']
 print(f'caller instance: {caller_instance}') # → obj
 print(f'caller from class: {type(caller_instance).__name__}') # → B
 print(f'caller from method: {function}') # → class_B_fun
 # alternatively:
 code = caller_frame.f_code
 assert function == code.co_name
 print(f'caller object file name or path: {filename}') # → 'foomodule.py'
 # alternatively:
 assert filename == code.co_filename
 module_name = getmodulename(filename)
 print(f'caller module: {module_name}') # → foomodule
class B(object):
 def class_B_fun(self): 
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename, stack
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals['self']
 print(f'caller instance: {caller_instance}') # → obj
 print(f'caller from class: {type(caller_instance).__name__}') # → B
 print(f'caller from method: {function}') # → class_B_fun
 # alternatively:
 code = caller_frame.f_code
 assert function == code.co_name
 print(f'caller object file name or path: {filename}') # → 'foomodule.py'
 # alternatively:
 assert filename == code.co_filename
 module_name = getmodulename(filename)
 print(f'caller module: {module_name}') # → foomodule
class B(object):
 def class_B_fun(self): 
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
from inspect import currentframe, getframeinfo, getmodulename
class A(object):
 def class_A_fun(self):
 cuurent_frame = currentframe()
 caller_frame = cuurent_frame.f_back
 filename, lineno, function, code_context, index = getframeinfo(caller_frame)
 # f_locals is the local namespace seen by the frame
 caller_instance = caller_frame.f_locals['self']
 print(f'caller instance: {caller_instance}') # → obj
 print(f'caller from class: {type(caller_instance).__name__}') # → B
 print(f'caller from method: {function}') # → class_B_fun
 # alternatively:
 code = caller_frame.f_code
 assert function == code.co_name
 print(f'caller object file name or path: {filename}') # → 'foomodule.py'
 # alternatively:
 assert filename == code.co_filename
 module_name = getmodulename(filename)
 print(f'caller module: {module_name}') # → foomodule
class B(object):
 def class_B_fun(self): 
 obj = A()
 obj.class_A_fun() 
if __name__ == "__main__":
 obj = B()
 obj.class_B_fun()
Source Link
AXO
  • 9.2k
  • 6
  • 73
  • 69
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /