Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16071

Re: a qustion about import that really confuses me

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'languages,': 0.03; 'distinct': 0.05; '__name__': 0.09; 'bug?': 0.09; 'constants.': 0.09; 'files:': 0.09; 'top-level': 0.09; 'utf-8': 0.09; 'am,': 0.12; 'def': 0.13; 'circular': 0.15; "'__main__':": 0.16; '-*-': 0.16; 'coding:': 0.16; 'obvious.': 0.16; 'received:192.168.1.104': 0.16; 'subject:import': 0.16; 'subject:really': 0.16; 'subtle.': 0.16; 'uppercase': 0.16; 'wanted.': 0.16; 'cc:addr:python-list': 0.16; 'language': 0.17; 'wrote:': 0.18; 'file,': 0.19; 'cc:no real name:2**0': 0.20; 'request,': 0.21; 'header:In-Reply-To:1': 0.22; 'appear': 0.23; 'module,': 0.23; 'cc:2**0': 0.24; 'there.': 0.24; 'code': 0.25; 'module': 0.26; 'import': 0.27; 'script': 0.28; 'convention': 0.29; 'explicitly': 0.29; 'script.': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'generally': 0.30; 'imported': 0.30; 'resources.': 0.30; 'source': 0.31; 'does': 0.32; 'values': 0.32; 'hi,': 0.32; 'earlier': 0.32; 'header:User- Agent:1': 0.33; 'there': 0.33; 'modules': 0.35; 'supposed': 0.35; 'similar': 0.36; 'problem.': 0.36; 'david': 0.36; 'two': 0.37; 'received:192': 0.37; 'another': 0.37; 'problems': 0.37; 'doing': 0.38; 'using': 0.38; 'same.': 0.39; 'should': 0.39; 'why': 0.39; "it's": 0.40; 'move': 0.40; 'received:192.168': 0.40; 'more': 0.61; 'your': 0.61; 'further,': 0.67; 'discover': 0.68; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'lucky': 0.74; "'main'": 0.84
Date Tue, 22 Nov 2011 08:16:26 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15
MIME-Version 1.0
To David Lu <davidlujun@gmail.com>
Subject Re: a qustion about import that really confuses me
References <CAAmfJ3CvPnS40+WT3MH-b7B7XZ9UtvjBHM6FEYaxi6UmtSKRQg@mail.gmail.com>
In-Reply-To <CAAmfJ3CvPnS40+WT3MH-b7B7XZ9UtvjBHM6FEYaxi6UmtSKRQg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:yTWjHV+6QRHcthR8pWtwOTA7zsJdzkDneCRprFCpnzT lDUDxvt9jjIM75nOsWMnmvRc+lkdG7CisZ3bLykmf3Fn/+Lu6M 9mv8jMb5opQOhTET0FzzhxLWZ5zTzj3bC5wlPalrI1io+VFc2H PZ40jVHnHqmCdviZirklM9QqbFAQo3VByPYrLfzpB+NQ6kh/Sd CT7prbIvN3Ds5rd74+3tOSGQN+53Tw+oHtzg8wc/Jj9RUM58qn zYCoQVjUSASChgStEYBC3U02WctpzsSLq4LRQvyzTIe3H9nYGd t93aPgZUyJGQlstU9BjPOMN178V7SqhFYcO4Hqr2aGMNygn6g= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2946.1321967821.27778.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321967821 news.xs4all.nl 6854 [2001:888:2000:d::a6]:50325
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16071

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: a qustion about import that really confuses me Dave Angel <d@davea.name> - 2011-11-22 08:16 -0500

csiph-web