forked from SigmaQuan/Better-Python-59-Ways
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_52_break_circular_dependencies.py
32 lines (23 loc) · 1.3 KB
/
item_52_break_circular_dependencies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Item 52: Know how to break circular dependencies
# Inevitably, while you're collaborating with others, you'll find a mutual
# interdependency between modules. It can even happen while you work by
# yourself on the various parts of a single program.
# There are three other ways to break circular dependencies.
# Reordering Imports
# The first approach is to change the order of imports.
# Import, Configure, Run
# A second solution to the circular imports problem is to have your modules
# minimize side effects at import time.
# Dynamic Import
# The third--and often simplest--solution to the circular imports problem is
# to use an import statement within a function or method. This is called a
# dynamic import because the module import happens while the program is
# running, not while the program is first starting up and initializing its
# modules.
# Things to remember
# 1. Circular dependencies happen when two modules must call into each other
# at import time. They can cause your program to crash at startup.
# 2. The best way to break a circular dependency is refactoring mutual
# dependencies into a separate module at the bottom of the dependency tree.
# 3. Dynamic imports are the simplest solution for breaking a circular
# dependency between modules while minimizing refactoring and complexity.