a qustion about import that really confuses me

David Lu davidlujun at gmail.com
Tue Nov 22 05:18:25 EST 2011


Hi, there.

I have two files:

a.py:

> # -*- coding: utf-8 -*-
> print('in a')
> import b
>
> print('var')
> VAR = 1
>
> def p():
>     print('{}, {}'.format(VAR, id(VAR)))
>
> if __name__ == '__main__':
>     VAR = -1
>     p()
>     b.p() # Where does this VAR come from?
>

b.py:

> # -*- coding: utf-8 -*-
> print('in b')
> import a
>
> def p():
>     a.p()
>

I don't understand why there're two different VARs, which is supposed to
the same.
Is it a bug?
If I move the 'main' block to another file, everything works well.

c.py:

> # coding=UTF-8
> import a
> import b
>
> if __name__ == '__main__':
>     a.VAR = -1
>     a.p()
>     b.p()
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111122/4a47f314/attachment.html>


More information about the Python-list mailing list