Python中的循环依赖 [英] Circular dependency in Python

查看:86
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆