- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 164字
- 2021-07-02 21:09:39
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.
- 微服務設計(第2版)
- Learn TypeScript 3 by Building Web Applications
- 從零構建知識圖譜:技術、方法與案例
- GeoServer Cookbook
- Blockly創意趣味編程
- Mastering OpenCV 4
- 精通Scrapy網絡爬蟲
- Elasticsearch for Hadoop
- Visual Basic程序設計實踐教程
- jQuery Mobile移動應用開發實戰(第3版)
- Bootstrap 4 Cookbook
- Learning Modular Java Programming
- JavaScript機器人編程指南
- App Inventor少兒趣味編程動手做
- C語言程序設計教程