Accessing Module variables from another Module

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Sep 5 08:22:29 EDT 2007


cjt22 at bath.ac.uk 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.



More information about the Python-list mailing list