in a function
def fun():pass
How does the function
know who called it?In a C language, it seems difficult. But in python it’s very simple
import traceback
def fun():
s = traceback.extract_stack()
print '%s Invoked me!'%s[-2][2]
the fun function will tell you who called it and print it out, so let’s try it:
>>> def a():fun()
>>> def b():fun()
>>> a()
a Invoked me!
>>> b()
b Invoked me!
>>>
div>
Read More:
- Python: Understanding__ str__
- Detailed explanation of Python__ new__() method
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- From in Python__ future__ The role of import *
- Typeerror: ‘module’ object is not callable
- Python2 PicklingError: Can‘t pickle <type ‘instancemethod‘>: attribute lookup __builtin__.instanceme
- Explanation of the function strip(), lstrip(), rstrip() in Python
- Error no module named ‘in newly installed Python_ sqlite3‘
- Yield usage in Python
- Python how does a. Py file call classes and functions in another. Py file
- python3 request module https certificate verify failed error
- python keyerror(0)
- In Python, print() prints to remove line breaks
- Python global variables and global keywords
- Simple Python crawler exercise: News crawling on sohu.com
- Solution: windowserror: [error 2]
- Implementation of Python switch / case statements
- Python program exit: OS_ Exit() and sys.exit ()
- Syntax error: invalid syntax before Python string
- Python switch / case statement implementation method