473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

There's GOT to be a better way!

I'm writing my first program where I call custom modules. The 'global'
command doesn't seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

Earl

Jul 18 '05 #1
10 1248
Earl Eiland wrote:
I'm writing my first program where I call custom modules. The 'global'
command doesn't seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

You are correct in assuming that global isn't what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don't forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module's attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #2
On Thu, 2005-03-03 at 15:11, Steve Holden wrote:
Earl Eiland wrote:
I'm writing my first program where I call custom modules. The 'global'
command doesn't seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

You are correct in assuming that global isn't what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don't forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module's attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.

Right. That part I figured out. How does one function in an imported
module access a variable in the same module?

module.py
def A():
test = 1
for x in range(10): B()

def B():
test = test + 1
main.py
import module
module.A()
This will fail, unless test is passed and returned.

Jul 18 '05 #3
Earl Eiland wrote:
On Thu, 2005-03-03 at 15:11, Steve Holden wrote:
Earl Eiland wrote:
I'm writing my first program where I call custom modules. The 'global'
command doesn't seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

You are correct in assuming that global isn't what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don't forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module's attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.


Right. That part I figured out. How does one function in an imported
module access a variable in the same module?

module.py
def A():

global test test = 1
for x in range(10): B()

def B(): global test test = test + 1
main.py
import module
module.A() print module.test

This will fail, unless test is passed and returned.


regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #4
Earl Eiland wrote:
module.py
def A():
test = 1
for x in range(10): B()

def B():
test = test + 1
main.py
import module
module.A()

This will fail, unless test is passed and returned.


(Sorry if this sent twice. It wasn't appearing for me the first time.)

You can use global here, though I wouldn't advise it.

---------- module.py ----------
def A():
global test
test = 1
for x in range(10):
B()
def B():
global test
test = test + 1
-------------------------------

py> import module
py> module.A()
py> module.test
11

This looks like it might be simpler with a class, e.g.:

---------- module.py ----------
class A(object):
def __init__(self):
self.test = 1
for x in range(10):
self.B()
def B(self):
self.test += 1
-------------------------------

py> import module
py> a = module.A()
py> a.test
11

STeVe
Jul 18 '05 #5
On Thu, 2005-03-03 at 16:43, Steve Holden wrote:
Earl Eiland wrote:
On Thu, 2005-03-03 at 15:11, Steve Holden wrote:
Earl Eiland wrote:

I'm writing my first program where I call custom modules. The 'global'
command doesn't seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?
You are correct in assuming that global isn't what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don't forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module's attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.


Right. That part I figured out. How does one function in an imported
module access a variable in the same module?

module.py
def A():

global test
test = 1
for x in range(10): B()

def B():

global test
test = test + 1
main.py
import module
module.A()

print module.test


This will fail, unless test is passed and returned.

I thought I tried that, and it didn't work. I must have made some other
mistake.

Thanks.

Jul 18 '05 #6
On Thu, 2005-03-03 at 16:46, Steven Bethard wrote:
Earl Eiland wrote:
module.py
def A():
test = 1
for x in range(10): B()

def B():
test = test + 1
main.py
import module
module.A()

This will fail, unless test is passed and returned.


(Sorry if this sent twice. It wasn't appearing for me the first time.)

You can use global here, though I wouldn't advise it.

---------- module.py ----------
def A():
global test
test = 1
for x in range(10):
B()
def B():
global test
test = test + 1
-------------------------------

py> import module
py> module.A()
py> module.test
11

This looks like it might be simpler with a class, e.g.:

---------- module.py ----------
class A(object):
def __init__(self):
self.test = 1
for x in range(10):
self.B()
def B(self):
self.test += 1
-------------------------------

py> import module
py> a = module.A()
py> a.test
11

STeVe


Guess I'm just gonna have to break down and figure out how to work with
classes!

Jul 18 '05 #7
This would be a good case to use OO design, imo. The following works
fine. Simply instantiate the object, call the method, and you can
access (and manipulate) the "module's" variable to your heart's
content.

module.py
class object:
def __init__(self):
self.test = 1
def A(self):
for x in range(10): self.B()

def B(self):
self.test = self.test + 1

main.py
from module import object

MyObj = object()
print MyObj.test
MyObj.A()
print MyObj.test

Jul 18 '05 #8
ga********@gmail.com wrote:
This would be a good case to use OO design, imo. The following works
fine. Simply instantiate the object, call the method, and you can
access (and manipulate) the "module's" variable to your heart's
content.

module.py
class object:


Ouch. Use a different name than 'object', which is the base class of all new-style classes.

Kent
Jul 18 '05 #9
Good call. I was just trying to be generic in my quickly put-together
example. It's been a while since I've used Python so these things I
easily forget :)

'object' here would be more aptly named 'MyObject'.

Jul 18 '05 #10
On Thu, 2005-03-03 at 16:43, Steve Holden wrote:
Earl Eiland wrote:
On Thu, 2005-03-03 at 15:11, Steve Holden wrote:

Earl Eiland wrote:

I'm writing my first program where I call custom modules. The 'global' command doesn't seem to apply, so how do I change a variable internally in a module without passing it down n layers, and then back out again?

You are correct in assuming that global isn't what you want - it really means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don't forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module's attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.

Right. That part I figured out. How does one function in an imported module access a variable in the same module?

module.py
def A():
global test
test = 1
for x in range(10): B()

def B():
global test
test = test + 1
main.py
import module
module.A()
print module.test
This will fail, unless test is passed and returned.

I thought I tried that, and it didn't work. I must have made some

other
mistake.

Thanks.

Jul 18 '05 #11

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

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
6
by: Michael | last post by:
Hi, I'm fairly new at Python, and have the following code that works but isn't very concise, is there a better way of writing it?? It seems much more lengthy than python code i have read..... :-)...
19
by: Rhek | last post by:
Hello, I would like to apologize for double posting this question because I posted this same question in what looks like the VB 6 newgroups and not the .Net newsgroup... Here goes: The code...
6
by: Nick Dreyer | last post by:
In VB.NET I would like to not have to create property get/set procedures for every class variable I want to expose to Excel VBA projects in COM builds. Can anyone tell me if that is possible, or...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
44
by: Tolga | last post by:
As far as I know, Perl is known as "there are many ways to do something" and Python is known as "there is only one way". Could you please explain this? How is this possible and is it *really* a...
3
by: baibaichen | last post by:
hi i want to output strings with indent space, the code looks like: std::set<std::string> files; std::set<std::string> iterator b = files.begin(); std::set<std::string> iterator e =...
2
by: yay_frogs | last post by:
Here is my problem: suppose there are, say, five events with these probabilities: event1 0.7 event2 0.1 event3 0.1 event4 0.05 event5 0.05 Note that sum of the probabilities is...
33
by: sklett | last post by:
I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this: "California" "_california" Here is the (ugly) solution I cam up...
4
by: kah | last post by:
Hi Guys, In my ASP.net application, i am display information using a dataset. e.g for each datarow in dataset.table(0).rows label.text=datarow.item("ColumnName").tostring next This works...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.