473,503 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Sep 5 '07 #1
3 2250
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.
Sep 5 '07 #2
On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
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" :)

Sep 5 '07 #3
cj***@bath.ac.uk a écrit :
On Sep 5, 1:22 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
>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' !-)

Sep 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
16725
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
17
1859
by: Jacek Generowicz | last post by:
I have a module whose behaviour needs to be configurable. The module needs to decide, the first time it is imported, beteween alternative interfaces it presents. Currently, I set some...
1
1215
by: subrahmanya raju via .NET 247 | last post by:
(Type your message here) I want to access the session variables in a module of my asp.net project. I want to use the session variables in the global functions written in the module ...
8
1930
by: Floris van Haaster | last post by:
Hi All! I have a question, i have a web application and I store some member information in a variable i declared in a module like: Public some_info_variable as string in module1.vb But...
9
5381
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
5
2686
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
32
5764
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
6
1382
by: Nathan Harmston | last post by:
Hi, I m playing around with extended euclids algorithm from Knuth. I m trying to build a function with a function inside it. def exteuclid(m,n): a,a1,b,b1,c,d = 0,1,1,0,m,n def euclid(c,d):...
7
1526
by: MD | last post by:
Hi, I would like to access "variables" defined in my Python program in a C module extension for Python. Is this possible? I looked at the Python C API reference but didn't find anything there...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.