How to know the caller (Python) in a function

xiaoxiao2021-03-06  63

In a function

DEF FUN (): Pass

How do this function know who is called? It seems to be difficult in Class C language. But in Python is very simple

Import TRACEBACK

Def fun (): s = traceback.extract_stack () Print '% s invoked me!'% s [-2] [2]

This Fun function can know who calls it, and print it out, let's try:

>>> DEF A (): fun ()

>>> DEF B (): fun ()

>>> a () a invoked me! >>> b () b INVOKED ME! >>>

OK! How to apply this feature? Play all the imagination.

转载请注明原文地址:https://www.9cbs.com/read-120206.html

New Post(0)