官术网_书友最值得收藏!

Resolving attribute errors raised due to cyclic dependencies

There are two common ways to address the attribute error. The first one is usually considered the best. This method is to break the cycle by taking some of the code from one of the modules and moving it into a third module, which both the other modules can import without causing a cycle. In the following example, if we move the A class into its own module, there would be no cycle and all would be well:

The other way to address this problem is to move the import statement that causes the cycle, as shown in the following screenshot:

If we move the import statement down, as shown in the preceding example, until it's below all the variable definitions that are needed by the other modules in this cycle, the module will be initialized enough when it's imported by the other modules. Python will still come back and finish the initialization later.

主站蜘蛛池模板: 鄂托克旗| 塘沽区| 定日县| 栾城县| 芮城县| 镇坪县| 镇雄县| 孝感市| 东港市| 甘孜| 都江堰市| 宜兴市| 湖口县| 蒙阴县| 图木舒克市| 濮阳县| 新龙县| 象州县| 金门县| 达日县| 巢湖市| 屏山县| 乡宁县| 安阳县| 富平县| 凌云县| 宁化县| 全南县| 浏阳市| 桃江县| 内乡县| 凌海市| 铁力市| 安庆市| 呼玛县| 佛教| 大兴区| 金门县| 德令哈市| 介休市| 江安县|