@bmispelon OK no it's much simpler, module a is simply partially initialized. By the time b imports it, it's not re-executed since it exists in sys.modules, and b imports every existing (yet) symbols within A. from a import A would work too.
pawamoy@fosstodon.org
Posts
-
Can someone explain this #Python import behavior -
Can someone explain this #Python import behavior@bmispelon by the way I'm not sure to understand why the circular import works. I think Python has special handling for some cases where it's able to tell the circular import is "safe" somehow (like "a is almost finished, there's only * to import from b", meaning b can import from a again, and when b is finisehd a is updated again with any symbols declared in b). Tried to find an actual answer in the past but didn't find anything. Maybe should read the sources!
-
Can someone explain this #Python import behaviorGot it! Did it in my head then verified with an interpreter

There's nothing weird here. Python executes stuff sequentially, so:
- in
from a import A
- in a: A = 1
- in a: from b import *
- in b: from a import * (so we have A = 1 in b)
- in b: A += 1 (so we have A = 2 in b)
- in a: finishing previous import, so we now have A = 2 in a
- in
finishing previous import, so we now have A = 2 in C
- in
print(A) -> 2!