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


Groups > comp.lang.python > #35227

Re: Strange effect with import

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'output': 0.04; 'subsequent': 0.04; 'cache': 0.05; '__name__': 0.07; 'python': 0.09; 'expectation': 0.09; 'here?': 0.09; 'imports': 0.09; 'instance.': 0.09; 'instantiated': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'files.': 0.13; 'static': 0.13; 'times,': 0.13; 'file,': 0.15; '"import': 0.16; '"real"': 0.16; "'r'": 0.16; "file's": 0.16; 'instances,': 0.16; "module's": 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'simulate': 0.16; 'subject:effect': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'wrote:': 0.17; 'module': 0.19; 'variable': 0.20; 'define': 0.20; 'bit': 0.21; 'import': 0.21; 'regardless': 0.21; 'smallest': 0.22; 'suddenly': 0.22; 'demonstrate': 0.23; 'this:': 0.23; "haven't": 0.23; 'seems': 0.23; 'random': 0.24; 'second': 0.24; 'script': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; '--------': 0.28; 'actual': 0.28; 'sensible': 0.29; 'case,': 0.29; 'class': 0.29; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'anywhere': 0.33; 'asked': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'or,': 0.34; 'third': 0.34; 'whatever': 0.35; 'similar': 0.35; 'there': 0.35; 'received:org': 0.36; 'explain': 0.36; 'but': 0.36; 'anything': 0.36; 'subject:with': 0.36; 'should': 0.36; 'problems': 0.36; 'possible': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'object': 0.38; 'things': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'kind': 0.61; 'between': 0.63; 'different': 0.63; 'more': 0.63; 'miss': 0.75; 'bitten': 0.84; 'definitive': 0.84; 'approach.': 0.91; 'by.': 0.91; 'hundred': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Strange effect with import
Date Thu, 20 Dec 2012 22:11:37 +0100
Organization None
References <ajhbbnFe4q1U1@mid.uni-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ada9.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.1111.1356037912.29569.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356037912 news.xs4all.nl 6859 [2001:888:2000:d::a6]:34582
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35227

Show key headers only | View raw


Jens Thoms Toerring wrote:

> Hi,
> 
>    I hope that this isn't a stupid question, asked already a
> hundred times, but I haven't found anything definitive on
> the problem I got bitten by. I have two Python files like
> this:
> 
> -------- S1.py ------
> import random
> import S2
> 
> class R( object ) :
>     r = random.random( )
> 
> if __name__ == "__main__" :
>     print R.r
>     S2.p( )
> 
> -------- S2.py ------
> import S1
> 
> def p( ) :
>     print S1.R.r
> 
> and my expectation was that the static variable 'r' of class
> R would be identical when accessed from S1.py and S2.py.
> Unfortunately, that isn't the case, the output is different
> (and R seems to get instantiated twice).
> 
> But when I define R in S2.py instead
> 
> -------- S1.py ------
> import S2
> 
> print S2.R.r
> S2.p( )
> 
> -------- S2.py ------
> import random
> 
> class R( object ) :
>     r = random.random( )
> 
> def p( ) :
>     print R.r
> 
> or, alternatively, if I put the defintion of class R into
> a third file which I then import from the other 2 files,
> things suddenly start to work as expected/ 

That's the correct approach.

> Can someone
> explain what's going one here? I found this a bit sur-
> prising.

You should never import your program's main module anywhere else in the 
program. When Python imports a module it looks it up by the module's name in 
the sys.modules cache. For the main script that name will be "__main__" 
regardless of the file's actual name, so a subsequent "import S2" will 
result in a cache miss and a new module instance.

Similar problems occur when there is a PYTHONPATH pointing into a package 
and you have both

import package.module

and

import module

Again you will end up with two module instances, one called 
"package.module", the other just "module".

> This is, of course, not my "real" code - it would be much
> more sensible to pass the number to the function in the
> second file as an argument - but is the smallest possinle
> program I could come up with that demonstrate the prob-
> lem. In my "real" code it's unfortunately not possible
> to pass that number to whatever is going to use it in the
>  other file, I have to simulate a kind of global variable
> shared between different files.


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Strange effect with import jt@toerring.de (Jens Thoms Toerring) - 2012-12-20 20:39 +0000
  Re: Strange effect with import Dave Angel <d@davea.name> - 2012-12-20 15:59 -0500
  Re: Strange effect with import Peter Otten <__peter__@web.de> - 2012-12-20 22:11 +0100
  Re: Strange effect with import Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-20 22:13 +0000
    Re: Strange effect with import jt@toerring.de (Jens Thoms Toerring) - 2012-12-20 22:52 +0000
      Re: Strange effect with import Terry Reedy <tjreedy@udel.edu> - 2012-12-20 19:54 -0500
        Re: Strange effect with import jt@toerring.de (Jens Thoms Toerring) - 2012-12-21 01:12 +0000
      Re: Strange effect with import Hans Mulder <hansmu@xs4all.nl> - 2012-12-21 01:54 +0100
        Re: Strange effect with import jt@toerring.de (Jens Thoms Toerring) - 2012-12-21 01:25 +0000
        Re: Strange effect with import jt@toerring.de (Jens Thoms Toerring) - 2012-12-21 15:52 +0000

csiph-web