Accessing Module variables from another Module

Daniel Larsson daniel.j.larsson at gmail.com
Wed Sep 5 07:47:50 EDT 2007


On 9/5/07, cjt22 at bath.ac.uk <cjt22 at bath.ac.uk> wrote:
>
> 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


No, don't introduce circular dependencies, and make it explicit what your
functions operate on by passing things as parameters, instead of using
global references.

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()


It is *much* more preferable to pass 'storeList' as a parameter to your
functions. Don't change things behind the scenes.

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?


Keep it the way it is. It's wrong on too many levels to do it the other way.

Cheers
> Chris
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070905/bfea5dea/attachment.html>


More information about the Python-list mailing list