Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

use class in class

35 views
Skip to first unread message

Robert Voigtländer

unread,
Jan 21, 2014, 5:20:48 AM1/21/14
to
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()

Chris Angelico

unread,
Jan 21, 2014, 5:47:15 AM1/21/14
to pytho...@python.org
On Tue, Jan 21, 2014 at 9:20 PM, Robert Voigtländer
<r.voigt...@gmail.com> wrote:
> 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

Robert Voigtländer

unread,
Jan 21, 2014, 6:11:27 AM1/21/14
to
> 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


Dave Angel

unread,
Jan 21, 2014, 7:04:31 AM1/21/14
to pytho...@python.org
Robert Voigtländer <r.voigt...@gmail.com> Wrote in message:
> 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.

>
>
>
>
> 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
>

--
DaveA

Robert Voigtländer

unread,
Jan 21, 2014, 10:24:53 AM1/21/14
to
>
> 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.
0 new messages