name space problem

B

BBands

An example:

class classA:
def __init__(self):
self.b = 1

def doStuff():
some calcs
a..b = 0

a = classA():
print a.b
doStuff()
print a.b

That works as hoped, printing 1, 0.

But, if I move doStuff to another module and:

import doStuff

class classA:
def __init__(self):
self.b = 1

a = classA()
print a.b
doStuff.doStuff()
print a.b

I get a 1 printed and an error: NameError: global name 'a' is not
defined

I think this is a name space issue, but I can't grok it.

Thanks in advance,

jab
 
M

marek.rocki

BBands napisa (a):
An example:

class classA:
def __init__(self):
self.b = 1

def doStuff():
some calcs
a..b = 0

a = classA():
print a.b
doStuff()
print a.b

That works as hoped, printing 1, 0.

But, if I move doStuff to another module and:

import doStuff

class classA:
def __init__(self):
self.b = 1

a = classA()
print a.b
doStuff.doStuff()
print a.b

I get a 1 printed and an error: NameError: global name 'a' is not
defined

I think this is a name space issue, but I can't grok it.

Thanks in advance,

jab

Hello. Indeed the doStuff function in the doStuff module can't do 'a.b
= 0' (the double dot was just a typo, right?) because it doesn't know
anything about an object named a.

I think the right solution would be not to use 'a' as a global
variable, but rather to pass it as an explicit parameter to the
function.

In module doStuff:

def doStuff(a):
# some calcs
a.b = 0

In the main module:

import doStuff
# ...
doStuff.doStuff(a)
 
B

BBands

Hello. Indeed the doStuff function in the doStuff module can't do 'a.b
= 0' (the double dot was just a typo, right?)
Yes.

because it doesn't know anything about an object named a.

I was trying to understand why it worked when written in, but not when
included.
I think the right solution would be not to use 'a' as a global
variable, but rather to pass it as an explicit parameter to the
function.

Does doing this make a copy of a?
In module doStuff:

def doStuff(a):
# some calcs
a.b = 0

In the main module:

import doStuff
# ...
doStuff.doStuff(a)

In my real ap a is quite large...

thanks,

jab
 
G

Gabriel Genellina

On Oct 23, 4:20 pm, (e-mail address removed) wrote:
I was trying to understand why it worked when written in, but not when
included.

Not *included*. When you do `import doStuff` you dont "include" anything;
this is what happens (simplified):

- Look into the already loaded modules; if a "doStuff" module already
exists, return it.
- Search some directories on disk, looking for "doStuff.py". If found,
load and execute it (remember that import, class, def, etc. are
*executable* statements in Python).
- If still not found, raise an error.

So, after successful execution of `import doStuff`, you have a new name
`doStuff` that refers to the imported module. It's not the "contents" of
doStuff that gets "included" into the current module; doStuff exists by
itself as a separate module, and you get a name that refers to it.
Does doing this make a copy of a?

No! Python never makes a copy unless you explicitely say so.
In my real ap a is quite large...

It doesn't matter. `a` is just a name, referencing an object. It's never
"copied" unless you specifically ask for it. Passing objects as arguments
is absolutely common, and the right thing to do in this case; it's
efficient, don't worry...
 
B

Bruno Desthuilliers

BBands a écrit :
I was trying to understand why it worked when written in, but not when
included.

because it's *not* "included" ? Python's imports are not includes, and
the notion of "global" namespace in Python really means module's namespace.
Does doing this make a copy of a?

No. Python nevers copy anything unless very explicitelly asked to. But
remember that names are locals to their namespaces, so that rebinding
'a' in doStuff won't affect the object originally bound to 'a' (mutating
the object bound to 'a' will of course work as expected).
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top