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

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.

主站蜘蛛池模板: 新竹市| 长海县| 南江县| 盐源县| 南安市| 卫辉市| 安吉县| 纳雍县| 贵州省| 保定市| 蛟河市| 社旗县| 谢通门县| 磐安县| 阿鲁科尔沁旗| 桃园县| 南溪县| 沈阳市| 漳州市| 凉山| 大港区| 商水县| 长汀县| 海兴县| 桃江县| 镇宁| 大名县| 三门峡市| 苏尼特左旗| 金沙县| 称多县| 盘锦市| 南通市| 盐源县| 衡阳县| 娄烦县| 阳原县| 长治县| 石门县| 莱芜市| 新巴尔虎左旗|