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

Importing a cyclic dependency

There is something that might trip us up when we're importing a module that shares the same package. Sometimes, the module we're importing wants to import us as well. This is called a cyclic dependency. When we try to import a cyclic dependency, we'll almost always get an attribute error exception, as in the following example:

That happens because when we ask Python to import the first module, Python immediately creates a module object for it and begins executing the code in the module.

That's fine, except that, when Python gets to the import statement for the next module in this cycle, it pauses running the code in the first module, leaving it not fully initialized. Even that isn't normally a problem because Python will come back and finish the initialization later.

However, when the second module asks to import the first module, Python just hands it the already allocated, and not fully initialized, module object. When the second module tries to access the variables stored in the first object, many of them will not yet have been created. Hence, an attribute error is raised.

主站蜘蛛池模板: 鹤庆县| 青田县| 珠海市| 江永县| 揭东县| 武穴市| 安泽县| 大洼县| 封丘县| 磐安县| 宾川县| 广平县| 赤壁市| 蓬安县| 塘沽区| 象州县| 循化| 从化市| 平安县| 博客| 肃宁县| 宜兰县| 滕州市| 彭泽县| 海城市| 会昌县| 华安县| 梅河口市| 石泉县| 景泰县| 霍林郭勒市| 长汀县| 富川| 海丰县| 佳木斯市| 共和县| 临沭县| 临猗县| 攀枝花市| 邯郸市| 孝昌县|