首页 > 解决方案 > 如何确定哪个 py 文件(以及该 py 文件中的哪个类)调用了另一个 py 文件?

问题描述

我有许多 py 文件(以及这些 py 文件中的类)正在调用另一个 py 文件。让我们将第一个 py 文件称为 A、B、C 和它们调用的 py 文件 Z。我想根据 A、B 或 C 是​​否调用该文件在 Z 中运行代码。有时,我从 B 调用 A,从 A 调用 Z。我想根据最后一个调用它的 py 文件在 Z 中运行代码。

这是一个例子:

A.py

import Z

class A1:
    def __init__(self):
        Z.Z1()

B.py
import Z

class B1:
    def __init__(self):
        Z.Z1()

C.py
Import Z
Import A

class C1:
    def __init__(self):
        Z.Z1()
    def callA(self):
        A.A1()

Z.py

if [the name of the method which called Z is A1 init]:
    x = some_abstract_class

if [the name of the method which called Z is B1 init]:
   x = some_other_abstract_class

class Z1(x):
   do_something...

在 callA 函数的情况下,我仍然想知道 A1 最后调用了 Z。为了让它工作,有什么东西可以代替方括号吗?我希望这会像name = "A1" 那样简单,但遗憾的是我不认为是这种情况。

谢谢,我希望这已经足够清楚了。尽管我从 Stackoverflow 中受益匪浅,但这是我第一次提出问题,希望我没有完全搞砸。

标签: pythonmodule

解决方案


推荐阅读