persisting data within a module

Discussion in 'Python' started by Peter J. Bismuti, Nov 12, 2007.

  1. I'm having trouble understanding how namespaces work in modules. I want to
    execute a module within the interpreter and then have values that are
    calculated persist so that other modules that get executed can retrieve them.

    For example, consider the two simple modules below. The first method fails
    and I'm not sure exactly why. (Note: assume one instance of an interpreter.
    In my case a 3rd party software tool that starts an interpreter when it
    launches).


    Two alternate ways of running it:

    1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver using
    the conditional statement if (__name__=="__main__"):

    test.py
    run2.py

    or,

    2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.

    run.py




    _________test.py__________________


    import sys,os

    A = 0

    def getA():
    global A
    return A

    def run():
    global A
    A = 10

    if (__name__=="__main__"):
    run()


    _________run.py__________________

    import test

    test.run()
    print "A = " + str(test.getA())



    _________run2.py__________________

    import test

    print "A = " + str(test.getA())

    --


    Peter Bismuti
    Boeing Information Technology
    Renton, WA
    (425) 234-0873 W
    (425) 442-7775 C
    Peter J. Bismuti, Nov 12, 2007
    #1
    1. Advertising

  2. Peter J. Bismuti

    Guest

    On Nov 12, 5:30 pm, "Peter J. Bismuti" <>
    wrote:
    > I'm having trouble understanding how namespaces work in modules. I want to
    > execute a module within the interpreter and then have values that are
    > calculated persist so that other modules that get executed can retrieve them.
    >


    Modules retain state their state across all imports in the same
    interpreter instance. Module state is not shared among different
    instances of the interpreter.

    > For example, consider the two simple modules below. The first method fails
    > and I'm not sure exactly why. (Note: assume one instance of an interpreter.
    > In my case a 3rd party software tool that starts an interpreter when it
    > launches).
    >
    > Two alternate ways of running it:
    >
    > 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver using
    > the conditional statement if (__name__=="__main__"):
    >
    > test.py
    > run2.py
    >


    Ok, what do you mean by this? Do you mean run test.py and then run
    run2.py? In so, then you will have *two* instances -- one for each
    file being executed. You can only have one main module per
    interpreter instance. I suspect this is the source of your confusion.

    > or,
    >
    > 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.
    >
    > run.py
    >
    > _________test.py__________________
    >
    > import sys,os
    >
    > A = 0
    >
    > def getA():
    > global A
    > return A
    >
    > def run():
    > global A
    > A = 10
    >
    > if (__name__=="__main__"):
    > run()
    >


    Here, A is only initialized when the module is loaded iff it is the
    main module. If it's not the main module, then it will have A set to
    0 until some other code calls run().

    > _________run.py__________________
    >
    > import test
    >
    > test.run()
    > print "A = " + str(test.getA())
    >


    This code calls test.run(), which is necessary for A to be 10.

    > _________run2.py__________________
    >
    > import test
    >
    > print "A = " + str(test.getA())
    >
    > --
    >


    This code gets the value of test.A without calling test.run(). Since
    test.run() was not called, A is the value it was initialized when the
    test module was loaded -- namely, 0.

    Hope this helps,

    --Nathan Davis
    , Nov 13, 2007
    #2
    1. Advertising

  3. I'm not sure how to better state my question than to post my code.

    The question boils down to which namespace to variable in the module (in this
    case A) end up in depending on whether or not the module is simply imported
    by another module which acts as the driver (run.py is __main__), or when the
    module drives itself (test.py __main__).

    For example: my guess is that in the first case A ends up in the namespace of
    test and could be referenced by test.A, in the second case A ends up in the
    global namespace (and therefore must be referred to as simply A by other
    modules).

    Can anyone please shed some light on this for me?

    Thanks



    > > I'm having trouble understanding how namespaces work in modules. I want
    > > to execute a module within the interpreter and then have values that are
    > > calculated persist so that other modules that get executed can retrieve
    > > them.

    >
    > Modules retain state their state across all imports in the same
    > interpreter instance. Module state is not shared among different
    > instances of the interpreter.
    >
    > > For example, consider the two simple modules below. The first method
    > > fails and I'm not sure exactly why. (Note: assume one instance of an
    > > interpreter. In my case a 3rd party software tool that starts an
    > > interpreter when it launches).
    > >
    > > Two alternate ways of running it:
    > >
    > > 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver
    > > using the conditional statement if (__name__=="__main__"):
    > >
    > > test.py
    > > run2.py

    >
    > Ok, what do you mean by this? Do you mean run test.py and then run
    > run2.py? In so, then you will have *two* instances -- one for each
    > file being executed. You can only have one main module per
    > interpreter instance. I suspect this is the source of your confusion.
    >
    > > or,
    > >
    > > 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.
    > >
    > > run.py
    > >
    > > _________test.py__________________
    > >
    > > import sys,os
    > >
    > > A = 0
    > >
    > > def getA():
    > > global A
    > > return A
    > >
    > > def run():
    > > global A
    > > A = 10
    > >
    > > if (__name__=="__main__"):
    > > run()

    >
    > Here, A is only initialized when the module is loaded iff it is the
    > main module. If it's not the main module, then it will have A set to
    > 0 until some other code calls run().
    >
    > > _________run.py__________________
    > >
    > > import test
    > >
    > > test.run()
    > > print "A = " + str(test.getA())

    >
    > This code calls test.run(), which is necessary for A to be 10.
    >
    > > _________run2.py__________________
    > >
    > > import test
    > >
    > > print "A = " + str(test.getA())
    > >
    > > --

    >
    > This code gets the value of test.A without calling test.run(). Since
    > test.run() was not called, A is the value it was initialized when the
    > test module was loaded -- namely, 0.
    >
    > Hope this helps,
    >
    > --Nathan Davis


    --


    Peter Bismuti
    Boeing Information Technology
    Renton, WA
    (425) 234-0873 W
    (425) 442-7775 C
    Peter J. Bismuti, Nov 13, 2007
    #3
  4. How is that state different depending on whether a module has been simply
    imported (#2. some other block of code has __name__ == "__main__") and the
    script itself being run (#1. and having __name__=="__main__")?

    Ultimately, what I want is for a module to remember (persist) the value of A,
    regardless of how the module has been loaded into the interpreter.

    Thanks

    > Modules retain state their state across all imports in the same
    > interpreter instance. Module state is not shared among different
    > instances of the interpreter.
    >
    > > For example, consider the two simple modules below. The first method
    > > fails and I'm not sure exactly why. (Note: assume one instance of an
    > > interpreter. In my case a 3rd party software tool that starts an
    > > interpreter when it launches).
    > >
    > > Two alternate ways of running it:
    > >
    > > 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver
    > > using the conditional statement if (__name__=="__main__"):
    > >
    > > test.py
    > > run2.py

    >
    > Ok, what do you mean by this? Do you mean run test.py and then run
    > run2.py? In so, then you will have *two* instances -- one for each
    > file being executed. You can only have one main module per
    > interpreter instance. I suspect this is the source of your confusion.
    >
    > > or,
    > >
    > > 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.
    > >
    > > run.py
    > >
    > > _________test.py__________________
    > >
    > > import sys,os
    > >
    > > A = 0
    > >
    > > def getA():
    > > global A
    > > return A
    > >
    > > def run():
    > > global A
    > > A = 10
    > >
    > > if (__name__=="__main__"):
    > > run()

    >
    > Here, A is only initialized when the module is loaded iff it is the
    > main module. If it's not the main module, then it will have A set to
    > 0 until some other code calls run().
    >
    > > _________run.py__________________
    > >
    > > import test
    > >
    > > test.run()
    > > print "A = " + str(test.getA())

    >
    > This code calls test.run(), which is necessary for A to be 10.
    >
    > > _________run2.py__________________
    > >
    > > import test
    > >
    > > print "A = " + str(test.getA())
    > >
    > > --

    >
    > This code gets the value of test.A without calling test.run(). Since
    > test.run() was not called, A is the value it was initialized when the
    > test module was loaded -- namely, 0.
    >
    > Hope this helps,
    >
    > --Nathan Davis


    --


    Peter Bismuti
    Boeing Information Technology
    Renton, WA
    (425) 234-0873 W
    (425) 442-7775 C
    Peter J. Bismuti, Nov 13, 2007
    #4
  5. En Tue, 13 Nov 2007 13:09:01 -0300, Peter J. Bismuti
    <> escribió:

    > How is that state different depending on whether a module has been simply
    > imported (#2. some other block of code has __name__ == "__main__") and
    > the
    > script itself being run (#1. and having __name__=="__main__")?


    It's not different at all, or I don't understand the question.

    > Ultimately, what I want is for a module to remember (persist) the value
    > of A,
    > regardless of how the module has been loaded into the interpreter.


    It doesn't care. If you have a variable A in (the global namespace of) a
    module, it's there, no matter how the module has been loaded. A namespace
    has no concept of "history", it's just a mapping from names to objects.

    Unless you're talking about this situation (should be on the FAQ, but I
    can't find it):

    --- begin one.py ---
    A = 1

    if __name__=='__main__':
    print "In one.py, A=", A
    import two
    print "In one.py, after importing two, A=", A
    --- end one.py

    --- begin two.py ---
    import one
    print "In two.py, one.A=", one.A
    one.A = 222
    print "In two.py, after modifying one.A=", one.A
    --- end one.py

    Executing:

    >python one.py


    you get this output:

    In one.py, A= 1
    In two.py, one.A= 1
    In two.py, after modifying one.A= 222
    In one.py, after importing two, A= 1

    In this (pathological) case, there are TWO different instances of the
    one.py module, because modules are indexed by name in the sys.modules
    dictionary, and the first instance is under the "__main__" name, and the
    second instance is under the "one" name.
    So: don't import the main script again.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 14, 2007
    #5
    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. Per Schjetne
    Replies:
    0
    Views:
    320
    Per Schjetne
    Oct 17, 2003
  2. Hai Nguyen

    Persisting Data Web Control Table??

    Hai Nguyen, Jan 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    329
    Craig Deelsnyder
    Jan 19, 2004
  3. Diane Yocom
    Replies:
    1
    Views:
    611
    =?Utf-8?B?UmljaA==?=
    May 12, 2004
  4. Nikron
    Replies:
    1
    Views:
    619
    Nikron
    Sep 28, 2007
  5. Nikron
    Replies:
    1
    Views:
    125
    Nikron
    Sep 28, 2007
Loading...

Share This Page