Source From Here
Question
If you have 2 functions like:
and
A calls B, can you get who is calling B inside B, like:
How-To
You can use the inspect module to get the calling stack. It returns a list of frame records. The third element in each record is the caller name. What you want is this:
Question
If you have 2 functions like:
- def A
- def B
- def A () :
- B ()
- def B () :
- this.caller.name
You can use the inspect module to get the calling stack. It returns a list of frame records. The third element in each record is the caller name. What you want is this:
- >>> import inspect
- >>> def f():
- ... for frame in inspect.stack():
- ... print(frame[3])
- ...
- >>> def g():
- ... f()
- ...
- >>> g()
- f
- g
沒有留言:
張貼留言