473,322 Members | 1,408 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,322 software developers and data experts.

NameError question - def(self,master) - master not in namespacewithin class?

Hi I am new to writing module and object oriented python code. I am
trying to understand namespaces and classes in python.

I have the following test case given in three files runner , master
and child. I am getting an error within child where in one line it
understands variable master.name and in the next line it gives a
NameError as given here

" print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined"
Sorry for a long post because I dont know how to frame my question.
I am pasting the code contained in the three files and the error
message here
Thanks for your help
harijay
The detailed error I get is
hazel:tmp hari$ python runner.py
Traceback (most recent call last):
File "runner.py", line 3, in <module>
import child
File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
class child():
File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined

#File runner.py
#!/usr/bin/python
import master
import child

if __name__=="__main__":
print "RUNNING RUNNER"
m = master.master("hj","oldhj")
s = child.child(m)
print "Now I have the variable master name %s and master.trash %s" %
(m.name , m.trash)

#File master.py
class master():
name=""
trash=""

def __init__(self,name,trash):
self.name = name
self.trash = trash
#File child.py
class child():
def __init__(self,master):
print "Master name is %s" % master.name
print "Now seeting master name to setnameinchild in child.py "
tmp = master.trash
master.trash = master.name
master.name = "setnameinchild"
print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
Oct 9 '08 #1
4 2930
On 9 Ott, 17:43, harijay <hari...@gmail.comwrote:
Hi I am new to writing module and object oriented python code. I am
trying to understand namespaces and classes in python.

I have the following test case given in three files runner , master
and child. I am getting an error within child where in one line it
understands variable master.name and in the next line it gives a
NameError as given here

" * *print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined"

Sorry for a long post because I dont know how to *frame my question.
I am pasting the code contained in the three files and the error
message here
Thanks for your help
harijay

The detailed error I get is
hazel:tmp hari$ python runner.py
Traceback (most recent call last):
* File "runner.py", line 3, in <module>
* * import child
* File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
* * class child():
* File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
* * print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined

#File runner.py
#!/usr/bin/python
import master
import child

if __name__=="__main__":
* * * * print "RUNNING RUNNER"
* * * * m = master.master("hj","oldhj")
* * * * s = child.child(m)
* * * * print "Now I have the variable master name %s and master.trash %s" %
(m.name , m.trash)

#File master.py
class master():
* * * * name=""
* * * * trash=""

* * * * def __init__(self,name,trash):
* * * * * * * * self.name = name
* * * * * * * * self.trash = trash

#File child.py
class child():
* * * * def __init__(self,master):
* * * * * * * * print "Master *name is %s" % master.name
* * * * * * * * print *"Now seeting master name to setnameinchild in child.py "
* * * * * * * * tmp = master.trash
* * * * * * * * master.trash = master.name
* * * * * * * * master.name = "setnameinchild"
* * * * print "Reset name now from %s to %s , oldname %s is savedin
mastertrash" % (master.trash, master.name , master.trash)

You need to have an import master in child.py too.

Ciao
-----
FB

Ciao
----
FB
Oct 9 '08 #2
On Thu, Oct 9, 2008 at 11:43 AM, harijay <ha*****@gmail.comwrote:
Hi I am new to writing module and object oriented python code. I am
trying to understand namespaces and classes in python.

I have the following test case given in three files runner , master
and child. I am getting an error within child where in one line it
understands variable master.name and in the next line it gives a
NameError as given here
<snip>
#File child.py
class child():
def __init__(self,master):
print "Master name is %s" % master.name
print "Now seeting master name to setnameinchild in child.py "
tmp = master.trash
master.trash = master.name
master.name = "setnameinchild"
print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
The last line (starting with print), is not indented to the correct
level. Assuming it's supposed to be part of the child class's
__init__ method, it needs to line up with the rest of that method.

--
Jerry
Oct 9 '08 #3
Thanks beiff for your prompt reply - But I shouldnt need to import
master in child.

Actually and very strangely. The problem fixed itself without any
change to my code.
I dont understand how. It may have been a problem with a bad *.pyc
lingering around . But now I cannot get the old NameError to repeat
itself..its very weird
Here is the new code and it runs just fine ..I am absolutely confused
as to why it failed several times in a row and then fixed itself.
Here is the new code just to convince myself that there is absolutely
no change;
#File runner.py
#!/usr/bin/python
import master
import child

if __name__=="__main__":
print "RUNNING RUNNER"
m = master.master("hj","oldhj")
s = child.child(m)
print "Now I have the variable master name %s and master.trash %s" %
(m.name , m.trash)
#File child.py
class child():

def __init__(self,master):
print "Master name is %s" % master.name
print "Now seeting master name to setnameinchild in child.py "
tmp = master.trash
master.trash = master.name
master.name = "setnameinchild"
print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)

#File master.py
class master():
name=""
trash=""

def __init__(self,name,trash):
self.name = name
self.trash = trash

Produces the output:
hazel:tmp hari$ python runner.py
RUNNING RUNNER
Master name is hj
Now seeting master name to setnameinchild in child.py
Reset name now from hj to setnameinchild , oldname hj is saved in
mastertrash
Now I have the variable master name setnameinchild and master.trash hj

On Oct 9, 11:54*am, bieff...@gmail.com wrote:
On 9 Ott, 17:43, harijay <hari...@gmail.comwrote:
Hi I am new to writing module and object oriented python code. I am
trying to understand namespaces and classes in python.
I have the following test case given in three files runner , master
and child. I am getting an error within child where in one line it
understands variable master.name and in the next line it gives a
NameError as given here
" * *print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined"
Sorry for a long post because I dont know how to *frame my question.
I am pasting the code contained in the three files and the error
message here
Thanks for your help
harijay
The detailed error I get is
hazel:tmp hari$ python runner.py
Traceback (most recent call last):
* File "runner.py", line 3, in <module>
* * import child
* File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
* * class child():
* File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
* * print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)
NameError: name 'master' is not defined
#File runner.py
#!/usr/bin/python
import master
import child
if __name__=="__main__":
* * * * print "RUNNING RUNNER"
* * * * m = master.master("hj","oldhj")
* * * * s = child.child(m)
* * * * print "Now I have the variable master name %s and master.trash %s" %
(m.name , m.trash)
#File master.py
class master():
* * * * name=""
* * * * trash=""
* * * * def __init__(self,name,trash):
* * * * * * * * self.name = name
* * * * * * * * self.trash = trash
#File child.py
class child():
* * * * def __init__(self,master):
* * * * * * * * print "Master *name is %s" % master.name
* * * * * * * * print *"Now seeting master name to setnameinchild in child.py "
* * * * * * * * tmp = master.trash
* * * * * * * * master.trash = master.name
* * * * * * * * master.name = "setnameinchild"
* * * * print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)

You need to have an import master in child.py too.

Ciao
-----
FB

Ciao
----
FB
Oct 9 '08 #4
Thanks Jerry and beiff ,
Jerry was right, it was an indent problem . Between using my text
editor and running from commandline something went out of sync and I
didnt catch it probably.
I can now reproduce the error with a bad ident .

These are my first posts to comp.lang.python..and I am very grateful
to everyone for their time .

harijay

On Oct 9, 12:07*pm, "Jerry Hill" <malaclyp...@gmail.comwrote:
On Thu, Oct 9, 2008 at 11:43 AM, harijay <hari...@gmail.comwrote:
Hi I am new to writing module and object oriented python code. I am
trying to understand namespaces and classes in python.
I have the following test case given in three files runner , master
and child. I am getting an error within child where in one line it
understands variable master.name and in the next line it gives a
NameError as given here

<snip>
#File child.py
class child():
* * * *def __init__(self,master):
* * * * * * * *print "Master *name is %s" % master.name
* * * * * * * *print *"Now seeting master name to setnameinchild in child.py "
* * * * * * * *tmp = master.trash
* * * * * * * *master.trash = master.name
* * * * * * * *master.name = "setnameinchild"
* * * *print "Reset name now from %s to %s , oldname %s is saved in
mastertrash" % (master.trash, master.name , master.trash)

The last line (starting with print), is not indented to the correct
level. *Assuming it's supposed to be part of the child class's
__init__ method, it needs to line up with the rest of that method.

--
Jerry
Oct 9 '08 #5

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

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
1
by: Chris | last post by:
As I understand it, I could create a class library, and then have some of my ASPX pages inherit from classes in the class library (the web application would have a reference to the class library...
12
by: Christoph Zwerschke | last post by:
Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in...
13
by: Allen | last post by:
I defines FileLogConstant class as following. class FileLogConstant { public: static const INT32 REGISTER_LOGGER; static const INT32 REGISTER_METHOD; static const INT32 MAX_LOGGER_COUNT;
8
by: Evan | last post by:
Hi I have a short script that makes 2 calls to methods in another script as follows: import canPlaces as canp callOne=canp.addMe(3,5) callTwo=canp.estocStn()
14
by: Gert Cuykens | last post by:
Is there a difference between <code> class HelloWorld: def index(self): index.exposed = True return "Hello world!" </code> and
4
by: jorgejch | last post by:
Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit. I'm getting the following error message <Atom.Atom object at 0x7f0b09597fd0>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.