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

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.

主站蜘蛛池模板: 万年县| 竹山县| 咸宁市| 延庆县| 体育| 措勤县| 仁布县| 泸水县| 建始县| 葵青区| 梁平县| 灯塔市| 徐州市| 图木舒克市| 钟祥市| 黑水县| 固原市| 天等县| 治多县| 开远市| 商南县| 济宁市| 上高县| 鹰潭市| 宁国市| 垣曲县| 九寨沟县| 镇平县| 乌兰察布市| 沽源县| 蒙山县| 桦南县| 红河县| 波密县| 兴城市| 新竹市| 临桂县| 铁岭市| 岚皋县| 右玉县| 谷城县|