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

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.

主站蜘蛛池模板: 颍上县| 宝山区| 阳东县| 溆浦县| 新巴尔虎左旗| 丹东市| 桑日县| 云和县| 巴青县| 宁德市| 新竹县| 仁怀市| 涿州市| 偃师市| 景洪市| 图片| 寿光市| 瓮安县| 平泉县| 林甸县| 建德市| 乌兰浩特市| 伊川县| 麻栗坡县| 南开区| 乐平市| 玉山县| 桦甸市| 方山县| 肥西县| 临城县| 手游| 八宿县| 遂昌县| 萨嘎县| 嘉祥县| 金寨县| 尖扎县| 且末县| 浦北县| 东港市|