Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16071 > unrolled thread
| Started by | Dave Angel <d@davea.name> |
|---|---|
| First post | 2011-11-22 08:16 -0500 |
| Last post | 2011-11-22 08:16 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: a qustion about import that really confuses me Dave Angel <d@davea.name> - 2011-11-22 08:16 -0500
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2011-11-22 08:16 -0500 |
| Subject | Re: a qustion about import that really confuses me |
| Message-ID | <mailman.2946.1321967821.27778.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web