-
-
Save mahmoud/32fd056a3d4d1cd03a4e8aeff6b5ee70 to your computer and use it in GitHub Desktop.
Usually circular imports are fine. Not great maybe, but ok. This pair of tiny files demonstrates how prematurely using a value is where the problem really manifests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import b | |
# print(b.val) | |
""" | |
If left commented, this pair of module is perfectly fine, no exceptions are raised. | |
If uncommented, you'll see a stack trace like: | |
Traceback (most recent call last): | |
File "a.py", line 2, in <module> | |
import b | |
File "/home/mahmoud/tmp/b.py", line 2, in <module> | |
import a | |
File "/home/mahmoud/tmp/a.py", line 4, in <module> | |
print(b.val) | |
AttributeError: 'module' object has no attribute 'val' | |
The module was created, but the value wasn't populated yet. The same would happen if you do | |
from b import val | |
The problem with that statement isn't the b import, but the val import. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import a | |
val = 'test' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment