a qustion about import that really confuses me
Dave Angel
d at davea.name
Tue Nov 22 08:16:26 EST 2011
On 11/22/2011 05:18 AM, David Lu wrote:
> 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
>>
Right there is your problem. You try to import a module that you've
earlier used as the top-level script. The top-level script gets a
module name of "__main__" and this import is defining a module of "a".
So you have two distinct modules from the same source file, as you've
discovered.
>> 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()
>>
More generally, you should avoid circular imports. Other problems can
occur, this is just the most blatant. When you discover that two
modules are using (importing) each other's resources. you should move as
much code as necessary from one of them to a 3rd module, and both should
import that one.
Similar problems appear in other languages, and the cure is generally
the same. Avoid circular dependencies.
Incidentally, using all uppercase for a name is a convention for
constants. Further, you explicitly request that VAR have different
values when it's the top-level script than when it's an imported module,
so the language is doing exactly what you request, even if not what you
wanted. You're lucky that the problem was so obvious. Many mutual
import problems are much more subtle.
--
DaveA
More information about the Python-list
mailing list