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

Discussion in 'Python' started by Stef Mientki, Oct 11, 2009.

  1. Stef Mientki

    Stef Mientki Guest

    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
     
    Stef Mientki, Oct 11, 2009
    #1
    1. Advertising

  2. Stef Mientki

    ryles Guest

    On Oct 10, 7:36 pm, Stef Mientki <> 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
     
    ryles, Oct 11, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    0
    Views:
    398
  2. plb
    Replies:
    0
    Views:
    318
  3. Michael Abbott

    Module executed twice when imported!

    Michael Abbott, Jun 28, 2006, in forum: Python
    Replies:
    10
    Views:
    480
    Georg Brandl
    Jul 1, 2006
  4. Dave Angel
    Replies:
    2
    Views:
    365
    Dave Angel
    Oct 12, 2009
  5. Peng Yu
    Replies:
    0
    Views:
    328
    Peng Yu
    Oct 31, 2009
Loading...

Share This Page