- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 189字
- 2021-07-02 21:09:39
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.
- Mastering Ext JS(Second Edition)
- Visual FoxPro程序設計教程
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- PHP 編程從入門到實踐
- 編寫高質量代碼:改善C程序代碼的125個建議
- 你必須知道的204個Visual C++開發問題
- HTML5+CSS3網站設計基礎教程
- Mastering JavaScript High Performance
- RISC-V體系結構編程與實踐(第2版)
- 單片機C語言程序設計實訓100例
- 計算機應用基礎教程(Windows 7+Office 2010)
- C++程序設計教程(第2版)
- Android編程權威指南(第4版)
- C/C++代碼調試的藝術(第2版)
- ASP.NET本質論