在一個函數中
def fun():
pass
這個函數如何知道是誰調用了它呢?
import traceback
def fun():
s = traceback.extract_stack()
print '%s Invoked me!'%s[-2][2]
這個 fun 函數就可以知道是誰調用了它,並打印出來:
def a():
fun()
def b():
fun()
a()
a Invoked me!
b()
b Invoked me!
在一個函數中
def fun():
pass
這個函數如何知道是誰調用了它呢?
import traceback
def fun():
s = traceback.extract_stack()
print '%s Invoked me!'%s[-2][2]
這個 fun 函數就可以知道是誰調用了它,並打印出來:
def a():
fun()
def b():
fun()
a()
a Invoked me!
b()
b Invoked me!
本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。