There's GOT to be a better way!

E

Earl Eiland

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
 
S

Steve Holden

Earl said:
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
 
E

Earl Eiland

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.
 
S

Steve Holden

Earl said:
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
 
S

Steven Bethard

Earl said:
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
 
E

Earl Eiland

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

Thanks.
 
E

Earl Eiland

(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!
 
G

gaudetteje

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
 
K

Kent Johnson

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
 
G

gaudetteje

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'.
 
E

Earl Eiland

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top