Accessing Module variables from another Module

Discussion in 'Python' started by cjt22@bath.ac.uk, Sep 5, 2007.

  1. Guest

    Hi

    I am new to Python (I have come from a large background of Java) and
    wondered if someone could explain to me how I can access variables
    stored in my main module to other functions within other modules
    called
    from this module

    for example
    file: main.py

    from Storage import store
    from Initialise import init
    from ProcessSteps import process

    storeList = store() #Creates a object to store Step objects
    init()
    process()

    file: Initialise.py
    def init()
    ......
    storeList.addStep([a,b,c])


    file: ProcessSteps.py
    def process()
    for step in storeList.stepList:
    etc

    I am currently just passing the variable in as a parameter
    but I thought with Python there would be a way to gain direct access
    to
    storeList within the modules called from the top main module?

    Cheers
    Chris
     
    , Sep 5, 2007
    #1
    1. Advertising

  2. a écrit :
    > Hi
    >
    > I am new to Python (I have come from a large background of Java) and
    > wondered if someone could explain to me how I can access variables
    > stored in my main module to other functions within other modules
    > called
    > from this module
    >

    (snip code)
    > I am currently just passing the variable in as a parameter


    And this is the RightThing(tm) to do.

    > but I thought with Python there would be a way to gain direct access
    > to
    > storeList within the modules called from the top main module?


    Yes : passing it as a param !-)

    For other modules to get direct access to main.storeList, you'd need
    these modules to import main in these other modules, which would
    introduce circular dependencies, which are something you usuallay don't
    want. Also, and while it's ok to read imported modules attributes, it's
    certainly not a good idea to mutate or rebind them IMHO, unless you're
    ok with spaghetti code.
     
    Bruno Desthuilliers, Sep 5, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
    > wrote:
    > a écrit :
    >
    > > Hi

    >
    > > I am new to Python (I have come from a large background of Java) and
    > > wondered if someone could explain to me how I can access variables
    > > stored in my main module to other functions within other modules
    > > called
    > > from this module

    >
    > (snip code)
    > > I am currently just passing the variable in as a parameter

    >
    > And this is the RightThing(tm) to do.
    >
    > > but I thought with Python there would be a way to gain direct access
    > > to
    > > storeList within the modules called from the top main module?

    >
    > Yes : passing it as a param !-)
    >
    > For other modules to get direct access to main.storeList, you'd need
    > these modules to import main in these other modules, which would
    > introduce circular dependencies, which are something you usuallay don't
    > want. Also, and while it's ok to read imported modules attributes, it's
    > certainly not a good idea to mutate or rebind them IMHO, unless you're
    > ok with spaghetti code.


    Cheers Bruno, I just wanted to make sure I was being consistent with
    the "Python way of things" :)
     
    , Sep 5, 2007
    #3
  4. a écrit :
    > On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
    > > wrote:
    >> a écrit :
    >>
    >>> Hi
    >>> I am new to Python (I have come from a large background of Java) and
    >>> wondered if someone could explain to me how I can access variables
    >>> stored in my main module to other functions within other modules
    >>> called
    >>> from this module

    >> (snip code)
    >>> I am currently just passing the variable in as a parameter

    >> And this is the RightThing(tm) to do.
    >>
    >>> but I thought with Python there would be a way to gain direct access
    >>> to
    >>> storeList within the modules called from the top main module?

    >> Yes : passing it as a param !-)
    >>
    >> For other modules to get direct access to main.storeList, you'd need
    >> these modules to import main in these other modules, which would
    >> introduce circular dependencies, which are something you usuallay don't
    >> want.


    (NB : the canonical solution to circular dependencies is to move the
    relevent definitions to a third module.)

    > Also, and while it's ok to read imported modules attributes, it's
    >> certainly not a good idea to mutate or rebind them IMHO, unless you're
    >> ok with spaghetti code.

    >
    > Cheers Bruno, I just wanted to make sure I was being consistent with
    > the "Python way of things" :)


    Then fire up your python shell and type 'import this' !-)
     
    Bruno Desthuilliers, Sep 5, 2007
    #4
    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. Salek Talangi

    Accessing variables of another class

    Salek Talangi, Jul 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    431
    Kevin Spencer
    Jul 28, 2003
  2. subrahmanya raju via .NET 247

    accessing session variables in a asp.net module

    subrahmanya raju via .NET 247, Jun 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,321
    Craig Deelsnyder
    Jun 26, 2004
  3. dwok
    Replies:
    7
    Views:
    720
    Hal Rosser
    Mar 4, 2005
  4. Replies:
    9
    Views:
    997
  5. Replies:
    1
    Views:
    110
Loading...

Share This Page