use class in class

  • Thread starter Robert Voigtländer
  • Start date
R

Robert Voigtländer

Hi,

I have a problem using a class object within another class.
It is about the line:

self.openlist.append(Node(self.start, None, 0, 0))

If I use it in __init__ it works. If I use it in calcRoute(self) I get the following error: local variable 'node' referenced before assignment The error occures in AMap.calcRoute()

Where is my mistake?


Thanks
Robert

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm

class Node(object):
def __init__(self, pos, parent, g , h):
self.pos = pos
self.parent = parent
self.g = g
self.h = h
self.f = g+h

class NewAMap(object):
def __init__(self, size, start, target):
self.size = size
self.start = start
self.target = target
self.openlist = []
self.closedlist = set()
self.EmptyValue = 0

self.clear()
self.addStart(self.start)
self.addTarget(self.target)

#self.openlist.append(Node(self.start, None, 0, 0))

def clear(self):
self.OccMap = np.zeros(shape=(self.size[0],self.size[1]),dtype=int)
def display(self):
print np.swapaxes(self.OccMap,0,1)

self.PicMap = np.zeros(shape=(self.size[0],self.size[1]),dtype=(float,3))
for x in xrange(0,self.size[0]):
for y in xrange(0,self.size[1]):
if self.OccMap[x][y] == 0:
self.PicMap[y][x]=(1,1,1)
elif self.OccMap[x][y] == -1:
self.PicMap[y][x]=(0,0,0)
elif self.OccMap[x][y] == -2:
self.PicMap[y][x]=(1,0,0)
elif self.OccMap[x][y] == -3:
self.PicMap[y][x]=(0,0,1)
#print self.PicMap
plt.imshow(self.PicMap, interpolation='nearest')
plt.show()

def addBlocked(self, blockposs):
self.OccMap[blockposs[0]][blockposs[1]]=-1
def addStart(self, start):
self.OccMap[start[0]][start[1]]=-2
def addTarget(self, target):
self.OccMap[target[0]][target[1]]=-3
def calcRoute(self):
self.openlist.append(Node(self.start, None, 0, 0))
for Node in self.openlist: print Node.pos, Node.parent, Node.g, Node.h, Node.f


def main():
AMap = NewAMap((20,20),(1,12),(12,12))
for y in range(8,17): AMap.addBlocked((8,y))
AMap.calcRoute()
AMap.display()

if __name__ == '__main__':
main()
 
C

Chris Angelico

def calcRoute(self):
self.openlist.append(Node(self.start, None, 0, 0))
for Node in self.openlist: print Node.pos, Node.parent, Node.g, Node.h, Node.f

You're using the name Node to mean two different things. In the first
line, you expect it to be the global name (which is the class), but on
the second, you want to iterate over the node instances. That assigns
to the name Node, which causes your problems.

I recommend using a different name for the instances here, probably
with a lower-case first letter. That would solve your problem _and_
make your code more readable.

ChrisA
 
R

Robert Voigtländer

I recommend using a different name for the instances here, probably
with a lower-case first letter. That would solve your problem _and_

make your code more readable.

Thanks a lot! I was confused by the debuger gifing me the wrong line as containing the error. I changed it regarding your advide. And it works.

Robert
 
D

Dave Angel

Robert Voigtländer said:
Hi,

I have a problem using a class object within another class.
It is about the line:

self.openlist.append(Node(self.start, None, 0, 0))

If I use it in __init__ it works. If I use it in calcRoute(self) I get the following error: local variable 'node' referenced before assignment The error occures in AMap.calcRoute()

Where is my mistake?

Chris has addressed your coding error. Within a function/method,
you really should use a name for just one purpose, and
especially if one of the purposes is global.

But you have a different problem as well. You're describing an
exception by retyping one of its lines, rather than using
copy/paste of the whole thing. The actual error message could not
have said "node", as there's no such name in the method.
 
R

Robert Voigtländer

copy/paste of the whole thing. The actual error message could not

have said "node", as there's no such name in the method.

You are correct. I copied the error before I renamed node into Node. I have to be more consistent here. :)
The source for the error was still the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top