471,348 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

name space problem

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

Oct 23 '07 #1
5 983
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)

Oct 23 '07 #2
On Oct 23, 4:20 pm, marek.ro...@wp.pl wrote:
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

Oct 24 '07 #3
En Tue, 23 Oct 2007 23:34:38 -0300, BBands <bb****@gmail.comescribió:
On Oct 23, 4:20 pm, marek.ro...@wp.pl 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.
>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?
No! Python never makes a copy unless you explicitely say so.
>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...
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...

--
Gabriel Genellina

Oct 24 '07 #4
BBands a écrit :
On Oct 23, 4:20 pm, marek.ro...@wp.pl wrote:
>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.
because it's *not* "included" ? Python's imports are not includes, and
the notion of "global" namespace in Python really means module's namespace.
>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?
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).

Oct 24 '07 #5
Thank you.

jab

Oct 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ja | last post: by
2 posts views Thread by Malcolm Dew-Jones | last post: by
1 post views Thread by alain MONTMORY | last post: by
1 post views Thread by Prashwee | last post: by
rajiv07
3 posts views Thread by rajiv07 | last post: by
13 posts views Thread by Rainy | last post: by
185 posts views Thread by jacob navia | last post: by
9 posts views Thread by ashokd001 | last post: by

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.