473,686 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__=="__ma in__":
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,n ame,trash):
self.name = name
self.trash = trash
#File child.py
class child():
def __init__(self,m aster):
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 2961
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__=="__ma in__":
* * * * 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,n ame,trash):
* * * * * * * * self.name = name
* * * * * * * * self.trash = trash

#File child.py
class child():
* * * * def __init__(self,m aster):
* * * * * * * * 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,m aster):
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__=="__ma in__":
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,m aster):
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,n ame,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__=="__ma in__":
* * * * 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,n ame,trash):
* * * * * * * * self.name = name
* * * * * * * * self.trash = trash
#File child.py
class child():
* * * * def __init__(self,m aster):
* * * * * * * * 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.pytho n..and I am very grateful
to everyone for their time .

harijay

On Oct 9, 12:07*pm, "Jerry Hill" <malaclyp...@gm ail.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,m aster):
* * * * * * * *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
4706
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 = IntVar() for part, list in info.masterList.items():
15
2590
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 ****************************************************************************** Hi fellow Python coders, I often find myself writing:: class grouping:
18
1529
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 the notion of "anonymous functions" go - partly because I use them all the time. Of course, one can always create a named function. But there are a lot of cases, such as multimethods / generics and other scenarios where functions are treated...
1
1395
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 DLL). But is it possible to make this work nicely in VS.NET, the way the default code behind pages work? I'm guessing not. I would like to be able to use the designer, drag a new button onto
12
7170
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 the following silly example (representative for more: class A:
13
1968
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
10538
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
5104
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
4251
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> Traceback (most recent call last): File "./Main.py", line 7, in <module> print (t.getAtomName()) File "/home/jorge/Documentos/projetos/mestrado/códigos/cartesian_zmatrix/Atom.py", line 22, in getAtomName return atomName
0
9054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7599
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6440
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4308
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.