Variable passing between modules.

Discussion in 'Python' started by Golawala, Moiz M (GE Infrastructure), Sep 8, 2004.

  1. Hi All,

    I want to pass a value between 2 modules. Both the modules are scripts (no classes involved). Does anyone know how I can do that. For eg:

    in module1


    if __name__ == '__main__':
    someVar = 'hello'
    import module2


    in module 2

    # here I would like to use someVar
    print someVar


    Thanks,
    Moiz Golawala
    GE Infrastructure, Security
    Software Engineer
    Enterprise Solutions

    T 561 994 5972
    F 561 994 6572
    E
    www.gesecurity.com

    791 Park of Commerce Blvd., Suite 100
    Boca Raton, FL, 33487, U.S.A.
    GE Security, Inc.
     
    Golawala, Moiz M (GE Infrastructure), Sep 8, 2004
    #1
    1. Advertising

  2. Golawala, Moiz M (GE Infrastructure)

    Jeff Shannon Guest

    Golawala, Moiz M (GE Infrastructure) wrote:

    >Hi All,
    >
    >I want to pass a value between 2 modules. Both the modules are scripts (no classes involved). Does anyone know how I can do that. For eg:
    >
    >in module1
    >
    >
    >if __name__ == '__main__':
    > someVar = 'hello'
    > import module2
    >
    >
    >in module 2
    >
    ># here I would like to use someVar
    >print someVar
    >
    >
    >



    The best way to do this is to put your module2 code inside a function,
    and simply call that function with someVar as an argument.

    --- module2.py -----

    def go(somevar):
    print somevar

    --- module1.py -----
    import module2
    somevar = "hello"
    module2.go(somevar)

    It *is* possible to insert a variable into another module's namespace,
    like so:

    import module2
    module2.somevar = somevar

    However, this won't accomplish what you want, because all of the code in
    module2 is executed when you import module2. If that code is all def
    statements, then you've created a bunch of functions that can be used
    later; however, if that code is all module-level imperative code, as you
    seem to be showing in your example, then it's all been executed by the
    time that module1 returns from the import statement. Inserting a
    variable into module2's namespace *will* let you use that variable as a
    global in any function in module2, but this has all of the drawbacks
    that globals always have plus a few more (tight coupling with module1,
    the potential to mistakenly think that rebinding module2.somevar will
    also rebind module1.somevar, etc)...

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Sep 8, 2004
    #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. John Baker
    Replies:
    3
    Views:
    684
    =?Utf-8?B?VmlzaG51LUNoaXZ1a3VsYQ==?=
    Feb 21, 2005
  2. oracle
    Replies:
    1
    Views:
    317
    Michael Berg
    Apr 22, 2005
  3. Chris
    Replies:
    4
    Views:
    305
    Michele Simionato
    May 22, 2004
  4. Golawala, Moiz M (GE Infrastructure)

    RE: Variable passing between modules.

    Golawala, Moiz M (GE Infrastructure), Sep 9, 2004, in forum: Python
    Replies:
    5
    Views:
    549
    Guest
    Sep 14, 2004
  5. pereges
    Replies:
    7
    Views:
    443
    pereges
    Jun 1, 2008
Loading...

Share This Page