473,287 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 1054
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ja | last post by:
Hello, I have a table that has a name field with the following data john doe john doe smith john d smith I need to separate the first, middle and last names into separate fields. ex....
2
by: Malcolm Dew-Jones | last post by:
Hello What is the correct way to associate the attributes with the tag if the tag has a name space associated with it and the attribute doesn't have a name space? i.e. <mytag
11
by: Eych | last post by:
I get a VB error when I try to update a table whose field I change with the following statement: dcUpdate.Dataset.Tables(0).Rows(0)("User Name") = Me.textbox1.Text if I change the field name...
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
1
by: Prashwee | last post by:
Hello All I have a simple problem with name space concept I have created an Add-in using a class library (let say the name of it is "ClassLibrary1") Then I added one form (say "Form1") and...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
3
rajiv07
by: rajiv07 | last post by:
Hi to all, I have a script to list the file names in a directory .When i run this script locally (command prompt) it displays the exact file name (even though the file name has two spaces).But i...
13
by: Rainy | last post by:
I have a stylistic question. In most languages words in var. name are separated by underscores or cap letters, resulting in var names like var_name, VarName and varName. I don't like that very much...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
9
by: ashokd001 | last post by:
Hi I am faceing a problem with a space in file name. I want to write a script which will open a file that file contain others file name, among them few file name contain space. >cat checkfile...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.