Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   code in a module is executed twice (cyclic import problems) ? (http://www.velocityreviews.com/forums/t701138-code-in-a-module-is-executed-twice-cyclic-import-problems.html)

Stef Mientki 10-10-2009 11:36 PM

code in a module is executed twice (cyclic import problems) ?
 
hello,

I always thought code in a module was only executed once,
but doesn't seem to be true.

I'm using Python 2.5.

And this is the example:

== A.py ==
My_List = []

== B.py ==
from A import *
My_List.append ( 3 )
print 'B', My_List
import C

== C.py ==
from A import *
from B import *
print 'C', My_List

Now when you start with B.py as the main program,
this is the resulting output:

B [3]
B [3, 3]
C [3, 3]

Why is the B.py executed twice ?

thanks,
Stef

ryles 10-11-2009 09:35 AM

Re: code in a module is executed twice (cyclic import problems) ?
 
On Oct 10, 7:36*pm, Stef Mientki <stef.mien...@gmail.com> wrote:
> hello,
>
> I always thought code in a module was only executed once,
> but doesn't seem to be true.
>
> I'm using Python 2.5.
>
> And this is the example:
>
> == A.py ==
> My_List = []
>
> == B.py ==
> from A import *
> My_List.append ( 3 )
> print 'B', My_List
> import C
>
> == C.py ==
> from A import *
> from B import *
> print 'C', My_List
>
> Now when you start with B.py as the main program,
> this is the resulting output:
>
> B [3]
> B [3, 3]
> C [3, 3]
>
> Why is the B.py executed twice ?
>
> thanks,
> Stef


FYI, there was actually a related discussion about this just recently:

http://groups.google.com/group/comp....4be42ecbee7cad


All times are GMT. The time now is 05:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.