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

Ned Batchelder ned at nedbatchelder.com
Sat May 17 20:16:01 EDT 2014


On 5/17/14 7:56 PM, varun7rs at 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 at 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




More information about the Python-list mailing list