Skip to content

Instantly share code, notes, and snippets.

@mahmoud
Created June 18, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmoud/32fd056a3d4d1cd03a4e8aeff6b5ee70 to your computer and use it in GitHub Desktop.
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.
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.
"""
import a
val = 'test'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment