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

ryles rylesny at gmail.com
Sun Oct 11 05:35:13 EDT 2009


On Oct 10, 7:36 pm, Stef Mientki <stef.mien... at 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.lang.python/browse_thread/thread/e24be42ecbee7cad



More information about the Python-list mailing list