python - 如何确定哪个 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 中受益匪浅,但这是我第一次提出问题,希望我没有完全搞砸。
解决方案
推荐阅读
- c++ - 逻辑私有 VS 物理私有
- linux - 根据模式条件添加2行的元素
- sql-server - sql server management studio中的远程调试
- php - 即使分配为无符号,Laravel 也无法添加外键
- javascript - javascript onClick中的函数()与函数问题
- apache-kafka - 如何在 Batch listener 错误处理场景中读取 Header 值
- linux-kernel - 实现AMG8xx红外相机内核模块和Video4Linux
- python - 使用 Pandas 中其他行的排列生成新行
- php - 使用 Symfony 使用 api 填充实体属性
- kotlin - 如果不为空,则取字符串未按预期工作