Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Accessing Module variables from another Module (http://www.velocityreviews.com/forums/t535016-accessing-module-variables-from-another-module.html)

cjt22@bath.ac.uk 09-05-2007 08:38 AM

Accessing Module variables from another Module
 
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


Bruno Desthuilliers 09-05-2007 12:22 PM

Re: Accessing Module variables from another Module
 
cjt22@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.

cjt22@bath.ac.uk 09-05-2007 12:55 PM

Re: Accessing Module variables from another Module
 
On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> cj...@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.


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


Bruno Desthuilliers 09-05-2007 01:30 PM

Re: Accessing Module variables from another Module
 
cjt22@bath.ac.uk a écrit :
> On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli...@wtf.websiteburo.oops.com> wrote:
>> cj...@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.


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



All times are GMT. The time now is 11:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.