Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71709 > unrolled thread

Can't figure out 'instance has no attribute' error

Started byvarun7rs@gmail.com
First post2014-05-17 16:56 -0700
Last post2014-05-19 05:10 +1000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Can't figure out 'instance has no attribute' error varun7rs@gmail.com - 2014-05-17 16:56 -0700
    Re: Can't figure out 'instance has no attribute' error Ned Batchelder <ned@nedbatchelder.com> - 2014-05-17 20:16 -0400
    Re: Can't figure out 'instance has no attribute' error "Rhodri James" <rhodri@wildebst.org.uk> - 2014-05-18 01:24 +0100
    Re: Can't figure out 'instance has no attribute' error Gary Herron <gary.herron@islandtraining.com> - 2014-05-17 17:16 -0700
    Re: Can't figure out 'instance has no attribute' error varun7rs@gmail.com - 2014-05-18 12:02 -0700
      Re: Can't figure out 'instance has no attribute' error Chris Angelico <rosuav@gmail.com> - 2014-05-19 05:10 +1000

#71709 — Can't figure out 'instance has no attribute' error

Fromvarun7rs@gmail.com
Date2014-05-17 16:56 -0700
SubjectCan't figure out 'instance has no attribute' error
Message-ID<fce5a884-c9b0-4870-bec3-bb8ce16fa5a9@googlegroups.com>
Hello Friends,

I am working on this code but I kind of get the same error over and over again. Could any of you help me fix this part of the error?

File RW1:
class PHY_NETWORK:
    def __init__(self, nodes, edges):
        self.nodes = nodes
        self.edges = edges  
        
	def addNode(self, node):
		self.nodes.append( node )

	def addEdge(self, edge):
		self.edges.append( edge )

File RW3:
def createnetwork(graph):

	
	doc = parse( args.paramFile )
	noderef = []	
	num = 0
	nodelist = doc.getElementsByTagName("node")
	for node in nodelist:
		noderef.append(node.getAttribute("id"))
		proc = random.randint(3500, 5000)
		stor = random.randint(7200, 8200)
		switch = random.randint(7000, 10000)
		num = num + 1
		totaldemands = random.randint(1, 5)
		graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
		    
		    
	linkid = 0    
	linklist = doc.getElementsByTagName("link")    
	for link in linklist :
		linkid = linkid + 1
		Source = link.getElementsByTagName("source") [0]
		Destination = link.getElementsByTagName("target") [0]
		Capacity = link.getElementsByTagName("capacity") [0]
		SourceID = noderef.index(Source.firstChild.data) + 1
		DestinationID = noderef.index(Destination.firstChild.data) + 1
		graph.addEdge( PHY_LINKS( linkid, Source.firstChild.data, Destination.firstChild.data, DestinationID, SourceID, float(Capacity.firstChild.data) ))


	global args
	args = parser.parse_args()

	samplenetwork = PHY_NETWORK([], [])
	for i in range(1, 100):
		createnetwork(samplenetwork)
		exportXmlFile(samplenetwork, args.output, 'a' )		
	
   
if __name__ == "__main__":
  main(sys.argv[1:])


srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
Traceback (most recent call last):
  File "RW3.py", line 157, in <module>
    main(sys.argv[1:])
  File "RW3.py", line 152, in main
    createnetwork(samplenetwork)
  File "RW3.py", line 31, in createnetwork
    graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
AttributeError: PHY_NETWORK instance has no attribute 'addNode'


The error that it give back is as above. I have the classes defined in RW1 file and I am importing the classes onto RW3 file and yet, It doesn't seem to work. I really am in need of your valuable suggestions. Thank You

[toc] | [next] | [standalone]


#71710

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-05-17 20:16 -0400
Message-ID<mailman.10101.1400372176.18130.python-list@python.org>
In reply to#71709
On 5/17/14 7:56 PM, varun7rs@gmail.com wrote:
> Hello Friends,
>
> I am working on this code but I kind of get the same error over and over again. Could any of you help me fix this part of the error?
>
> File RW1:
> class PHY_NETWORK:
>     def __init__(self, nodes, edges):
>         self.nodes = nodes
>         self.edges = edges
>
> 	def addNode(self, node):
> 		self.nodes.append( node )
>
> 	def addEdge(self, edge):
> 		self.edges.append( edge )
>
...
>
>
> srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
> Traceback (most recent call last):
>    File "RW3.py", line 157, in <module>
>      main(sys.argv[1:])
>    File "RW3.py", line 152, in main
>      createnetwork(samplenetwork)
>    File "RW3.py", line 31, in createnetwork
>      graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
> AttributeError: PHY_NETWORK instance has no attribute 'addNode'
>
>
> The error that it give back is as above. I have the classes defined in RW1 file and I am importing the classes onto RW3 file and yet, It doesn't seem to work. I really am in need of your valuable suggestions. Thank You
>

You've set your editor to display tabs as 4 spaces, but then you've 
sometimes used tabs in your file, and sometimes 4 spaces.  Look at how 
your code is indented in your post: the addNode and addEdge definitions 
are indented with tab characters instead of spaces, so Python thinks 
they are eight spaces in.  This makes them defined inside of __init__, 
rather than as part of your class.

Set your editor to insert spaces when you use the Tab key, and set it to 
use 4-space indents.  Then find all the tab characters in your file and 
replace them with the proper number of spaces.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#71712

From"Rhodri James" <rhodri@wildebst.org.uk>
Date2014-05-18 01:24 +0100
Message-ID<op.xf04rvvv5079vu@gnudebeest>
In reply to#71709
On Sun, 18 May 2014 00:56:42 +0100, <varun7rs@gmail.com> wrote:

> Hello Friends,
>
> I am working on this code but I kind of get the same error over and over  
> again. Could any of you help me fix this part of the error?

Shuffling your post around to make an explanation easier, the traceback is:

> srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
> Traceback (most recent call last):
>   File "RW3.py", line 157, in <module>
>     main(sys.argv[1:])
>   File "RW3.py", line 152, in main
>     createnetwork(samplenetwork)
>   File "RW3.py", line 31, in createnetwork
>     graph.addNode(PHY_NODES( node.getAttribute("id"), int(num),  
> float(xCoordinates.firstChild.data),  
> float(yCoordinates.firstChild.data), float(proc), float(stor),  
> float(switch), int(totaldemands)))
> AttributeError: PHY_NETWORK instance has no attribute 'addNode'

So Python thinks that PHY_NETWORK has no "addNode", but you do.  Do you  
perchance have the tab width in your editor set to 4?  I ask, because I  
imagine that you see this:

> File RW1:
> class PHY_NETWORK:
>     def __init__(self, nodes, edges):
>         self.nodes = nodes
>         self.edges = edges
>     def addNode(self, node):
>         self.nodes.append( node )
[snippety snip]

I however saw your post like this:

> File RW1:
> class PHY_NETWORK:
>     def __init__(self, nodes, edges):
>         self.nodes = nodes
>         self.edges = edges
>         def addNode(self, node):
> 		        self.nodes.append( node )
[snippety snip]

I've replaced the tabs with spaces to make it clearer.  Basically, you've  
got a mix of tabs and spaces, which is always a bad idea, and as a result  
Python thinks that addNode is an attribute of PHY_NETWORK.__init__, not of  
PHY_NETWORK.  You need to go through and replace all your tab characters  
with four spaces, and stop using tabs.

-- 
Rhodri James *-* Wildebeest Herder to the Masses

[toc] | [prev] | [next] | [standalone]


#71713

FromGary Herron <gary.herron@islandtraining.com>
Date2014-05-17 17:16 -0700
Message-ID<mailman.10103.1400372803.18130.python-list@python.org>
In reply to#71709
On 05/17/2014 04:56 PM, varun7rs@gmail.com wrote:
> Hello Friends,
>
> I am working on this code but I kind of get the same error over and over again. Could any of you help me fix this part of the error?

It's an indentation error:  In the following the three function defs for 
__init__, addNode and addEdge should all be at the same indentation 
level.  Instead, you have the later two defined *inside* the __init__.

Gary Herron


>
> File RW1:
> class PHY_NETWORK:
>      def __init__(self, nodes, edges):
>          self.nodes = nodes
>          self.edges = edges
>          
> 	def addNode(self, node):
> 		self.nodes.append( node )
>
> 	def addEdge(self, edge):
> 		self.edges.append( edge )
>
> File RW3:
> def createnetwork(graph):
>
> 	
> 	doc = parse( args.paramFile )
> 	noderef = []	
> 	num = 0
> 	nodelist = doc.getElementsByTagName("node")
> 	for node in nodelist:
> 		noderef.append(node.getAttribute("id"))
> 		proc = random.randint(3500, 5000)
> 		stor = random.randint(7200, 8200)
> 		switch = random.randint(7000, 10000)
> 		num = num + 1
> 		totaldemands = random.randint(1, 5)
> 		graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
> 		
> 		
> 	linkid = 0
> 	linklist = doc.getElementsByTagName("link")
> 	for link in linklist :
> 		linkid = linkid + 1
> 		Source = link.getElementsByTagName("source") [0]
> 		Destination = link.getElementsByTagName("target") [0]
> 		Capacity = link.getElementsByTagName("capacity") [0]
> 		SourceID = noderef.index(Source.firstChild.data) + 1
> 		DestinationID = noderef.index(Destination.firstChild.data) + 1
> 		graph.addEdge( PHY_LINKS( linkid, Source.firstChild.data, Destination.firstChild.data, DestinationID, SourceID, float(Capacity.firstChild.data) ))
>
>
> 	global args
> 	args = parser.parse_args()
>
> 	samplenetwork = PHY_NETWORK([], [])
> 	for i in range(1, 100):
> 		createnetwork(samplenetwork)
> 		exportXmlFile(samplenetwork, args.output, 'a' )		
> 	
>     
> if __name__ == "__main__":
>    main(sys.argv[1:])
>
>
> srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
> Traceback (most recent call last):
>    File "RW3.py", line 157, in <module>
>      main(sys.argv[1:])
>    File "RW3.py", line 152, in main
>      createnetwork(samplenetwork)
>    File "RW3.py", line 31, in createnetwork
>      graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
> AttributeError: PHY_NETWORK instance has no attribute 'addNode'
>
>
> The error that it give back is as above. I have the classes defined in RW1 file and I am importing the classes onto RW3 file and yet, It doesn't seem to work. I really am in need of your valuable suggestions. Thank You

[toc] | [prev] | [next] | [standalone]


#71735

Fromvarun7rs@gmail.com
Date2014-05-18 12:02 -0700
Message-ID<b0c945f5-d49c-4a05-9124-616c64a529fa@googlegroups.com>
In reply to#71709
On Sunday, 18 May 2014 01:56:42 UTC+2, varu...@gmail.com  wrote:
> Hello Friends,
> 
> 
> 
> I am working on this code but I kind of get the same error over and over again. Could any of you help me fix this part of the error?
> 
> 
> 
> File RW1:
> 
> class PHY_NETWORK:
> 
>     def __init__(self, nodes, edges):
> 
>         self.nodes = nodes
> 
>         self.edges = edges  
> 
>         
> 
> 	def addNode(self, node):
> 
> 		self.nodes.append( node )
> 
> 
> 
> 	def addEdge(self, edge):
> 
> 		self.edges.append( edge )
> 
> 
> 
> File RW3:
> 
> def createnetwork(graph):
> 
> 
> 
> 	
> 
> 	doc = parse( args.paramFile )
> 
> 	noderef = []	
> 
> 	num = 0
> 
> 	nodelist = doc.getElementsByTagName("node")
> 
> 	for node in nodelist:
> 
> 		noderef.append(node.getAttribute("id"))
> 
> 		proc = random.randint(3500, 5000)
> 
> 		stor = random.randint(7200, 8200)
> 
> 		switch = random.randint(7000, 10000)
> 
> 		num = num + 1
> 
> 		totaldemands = random.randint(1, 5)
> 
> 		graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
> 
> 		    
> 
> 		    
> 
> 	linkid = 0    
> 
> 	linklist = doc.getElementsByTagName("link")    
> 
> 	for link in linklist :
> 
> 		linkid = linkid + 1
> 
> 		Source = link.getElementsByTagName("source") [0]
> 
> 		Destination = link.getElementsByTagName("target") [0]
> 
> 		Capacity = link.getElementsByTagName("capacity") [0]
> 
> 		SourceID = noderef.index(Source.firstChild.data) + 1
> 
> 		DestinationID = noderef.index(Destination.firstChild.data) + 1
> 
> 		graph.addEdge( PHY_LINKS( linkid, Source.firstChild.data, Destination.firstChild.data, DestinationID, SourceID, float(Capacity.firstChild.data) ))
> 
> 
> 
> 
> 
> 	global args
> 
> 	args = parser.parse_args()
> 
> 
> 
> 	samplenetwork = PHY_NETWORK([], [])
> 
> 	for i in range(1, 100):
> 
> 		createnetwork(samplenetwork)
> 
> 		exportXmlFile(samplenetwork, args.output, 'a' )		
> 
> 	
> 
>    
> 
> if __name__ == "__main__":
> 
>   main(sys.argv[1:])
> 
> 
> 
> 
> 
> srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
> 
> Traceback (most recent call last):
> 
>   File "RW3.py", line 157, in <module>
> 
>     main(sys.argv[1:])
> 
>   File "RW3.py", line 152, in main
> 
>     createnetwork(samplenetwork)
> 
>   File "RW3.py", line 31, in createnetwork
> 
>     graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), float(proc), float(stor), float(switch), int(totaldemands)))
> 
> AttributeError: PHY_NETWORK instance has no attribute 'addNode'
> 
> 
> 
> 
> 
> The error that it give back is as above. I have the classes defined in RW1 file and I am importing the classes onto RW3 file and yet, It doesn't seem to work. I really am in need of your valuable suggestions. Thank You

Thank you very much Ned, Rodri and Gary. I changed the settings of gedit text editor as mentioned in the Zed Shaw tutorial. I think this is causing me the problem. I'll follow your advice.

[toc] | [prev] | [next] | [standalone]


#71736

FromChris Angelico <rosuav@gmail.com>
Date2014-05-19 05:10 +1000
Message-ID<mailman.10114.1400440221.18130.python-list@python.org>
In reply to#71735
On Mon, May 19, 2014 at 5:02 AM,  <varun7rs@gmail.com> wrote:
> Thank you very much Ned, Rodri and Gary. I changed the settings of gedit text editor as mentioned in the Zed Shaw tutorial. I think this is causing me the problem. I'll follow your advice.
>

I find that there are better editors than gedit. My personal
preference is SciTE; one of its syntax highlighting features is a nice
big warning if the indentation doesn't make sense from one line to the
next. So if you switch from spaces to tabs, or mismatch indents, or
anything, it gives you this blue marker right through the faulty
indented section. Pick an editor with that kind of feature, and you'll
find your job way easier.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web