use class in class

Chris Angelico rosuav at gmail.com
Tue Jan 21 05:47:15 EST 2014


On Tue, Jan 21, 2014 at 9:20 PM, Robert Voigtländer
<r.voigtlaender at 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



More information about the Python-list mailing list