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


Groups > comp.lang.python > #35224

Re: Strange effect with import

Date 2012-12-20 15:59 -0500
From Dave Angel <d@davea.name>
Subject Re: Strange effect with import
References <ajhbbnFe4q1U1@mid.uni-berlin.de>
Newsgroups comp.lang.python
Message-ID <mailman.1109.1356037191.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 12/20/2012 03:39 PM, 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

You have a big problem right here.  You have two modules importing each
other.  Any time you have direct or indirect mutual imports, you have
the potential for trouble.

That trouble gets much worse since you are actually running one of these
as a script.  Presumably you're running S1.py as a script.  The script's
module object is NOT the same one as the other module S2 gets by
importing S1.  Don't do that.

Move the common code into a third module, and import that one from both
places.  Then it'll only exist once.



> 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/ Can someone
> explain what's going one here? I found this a bit sur-
> prising.
>
> 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.
>
>                         Best regards, Jens


-- 

DaveA

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