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

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.

主站蜘蛛池模板: 阳春市| 乌鲁木齐市| 沙雅县| 阿拉善左旗| 环江| 哈密市| 和龙市| 中山市| 定陶县| 长宁区| 余江县| 桐乡市| 通辽市| 民丰县| 平江县| 习水县| 巍山| 吴忠市| 九龙坡区| 荆门市| 游戏| 甘泉县| 乐安县| 晋中市| 琼海市| 商都县| 新乡县| 平利县| 孝义市| 揭东县| 革吉县| 噶尔县| 桐庐县| 遵义县| 门头沟区| 青冈县| 泸州市| 囊谦县| 兰坪| 台中县| 如东县|