Python中的循环依赖 [英] Circular dependency in Python
问题描述
我有两个文件 node.py
和 path.py
,它们定义了两个类, Node
和 Path
。
I have two files, node.py
and path.py
, which define two classes, Node
and Path
, respectively.
到今天为止,定义路径
的对象引用了 Node
对象,因此我已经完成了
Up to today, the definition for Path
referenced the Node
object, and therefore I had done
from node.py import *
code> path.py 文件。
in the path.py
file.
但是,到目前为止,我为 Node创建了一种新方法
引用 Path
对象。
However, as of today I created a new method for Node
that references the Path
object.
尝试导入<$时遇到问题c $ c> path.py :我尝试过,当程序运行并调用使用的
,关于未定义 Path
方法时节点节点
的异常上升。
I had problems when trying to import path.py
: I tried it, and when the program ran and called the Path
method that uses Node
, an exception rose about Node
not being defined.
我该怎么办?
推荐答案
导入Python模块 是一篇很棒的文章,解释了循环
Importing Python Modules is a great article that explains circular imports in Python.
最简单的解决方法是将路径导入移动到节点模块的末尾。
The easiest way to fix this is to move the path import to the end of the node module.
这篇关于Python中的循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!