import inspect
def foo():
print(inspect.stack()[0][3])
print(inspect.stack()[1][3]) # will give the caller of foos name, if something called foo
foo()
output:
foo <module_caller_of_foo>
More Related Contents:
- How to get current function into a variable?
- How function is getting called without defining in python [closed]
- Check if input is empty
- Unexpected returned variable in Python [duplicate]
- Why does my recursive function return None?
- Creating functions in a loop
- What is the naming convention in Python for variable and function names?
- How to get the caller’s method name in the called method?
- How can I find the number of arguments of a Python function?
- Finding what methods a Python object has
- Understanding Python’s call-by-object style of passing function arguments
- Python – Passing a function into another function
- Builtin function not working with Spyder
- Python: dynamically create function at runtime
- Get all object attributes in Python? [duplicate]
- Run PowerShell function from Python script
- Do something every x (milli)seconds in pygame
- Set function signature in Python
- How to execute a function asynchronously every 60 seconds in Python?
- How to have multiple conditions for one if statement in python [duplicate]
- Check if two Python functions are equal
- How can I more easily suppress previous exceptions when I raise my own exception in response?
- How can I use `return` to get back multiple values from a loop? Can I put them in a list?
- What is first class function in Python
- Get a list/tuple/dict of the arguments passed to a function?
- numpy.r_ is not a function. What is it?
- Is there a generic way for a function to reference itself?
- How to watch for a variable change in python without dunder setattr or pdb
- Modify global list inside a function
- Sort python list by function