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


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

use class in class

Started byRobert Voigtländer <r.voigtlaender@gmail.com>
First post2014-01-21 02:20 -0800
Last post2014-01-21 07:24 -0800
Articles 5 — 3 participants

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


Contents

  use class in class Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 02:20 -0800
    Re: use class in class Chris Angelico <rosuav@gmail.com> - 2014-01-21 21:47 +1100
      Re: use class in class Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 03:11 -0800
    Re:use class in class Dave Angel <davea@davea.name> - 2014-01-21 07:04 -0500
      Re: use class in class Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 07:24 -0800

#64394 — use class in class

FromRobert Voigtländer <r.voigtlaender@gmail.com>
Date2014-01-21 02:20 -0800
Subjectuse class in class
Message-ID<8abb6a72-c258-4f08-a391-867d2680591d@googlegroups.com>
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()

[toc] | [next] | [standalone]


#64396

FromChris Angelico <rosuav@gmail.com>
Date2014-01-21 21:47 +1100
Message-ID<mailman.5775.1390301245.18130.python-list@python.org>
In reply to#64394
On Tue, Jan 21, 2014 at 9:20 PM, Robert Voigtländer
<r.voigtlaender@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

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


#64398

FromRobert Voigtländer <r.voigtlaender@gmail.com>
Date2014-01-21 03:11 -0800
Message-ID<4b31ea37-a099-4e3e-a87a-332c16a16cc1@googlegroups.com>
In reply to#64396
> 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

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


#64406

FromDave Angel <davea@davea.name>
Date2014-01-21 07:04 -0500
Message-ID<mailman.5781.1390305745.18130.python-list@python.org>
In reply to#64394
 Robert Voigtländer <r.voigtlaender@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

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


#64418

FromRobert Voigtländer <r.voigtlaender@gmail.com>
Date2014-01-21 07:24 -0800
Message-ID<5af3e509-6d01-4b77-b832-0ebea77cddf9@googlegroups.com>
In reply to#64406
> 
>  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.

[toc] | [prev] | [standalone]


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


csiph-web