SO pyton gurus! I've just found an astonishing phenomenon that I don't understand. The problem can be best shown as code:
#== kid.py ==#
import dad
def spam ():
dad.spam()
#== dad.py ==#
import kid
x = 1
print "body", x
x = 2
def spam ():
print "spam", x
if __name__ == '__main__':
x = 3
spam()
kid.spam()
print "main", x
I'm using Python 2.7.3. Can you guess the output of python dad.py
? The answer is (I wish SO had a spoiler shading tag) body 1 body 1 spam 3 spam 2 main 3
. So could you explain
- Why is
body 1
printed twice? - How can
dad.x != kid.dad.x
be? - If I really need to make the two modules import each other, how can I modify it to get
kid.dad.x
properly updated?