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


Groups > comp.lang.python > #12770

Re: Need help with simple OOP Python question

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Need help with simple OOP Python question
Followup-To comp.lang.python
Date 2011-09-05 16:43 +0200
Organization None
Message-ID <j42n5g$qm5$1@solani.org> (permalink)
References <dce02da0-c9c9-4331-aa2e-7d99f5e26cd1@g9g2000yqb.googlegroups.com> <87sjobo3es.fsf@benfinney.id.au> <93d65d9e-8a15-4423-94c0-3d385def24ed@et6g2000vbb.googlegroups.com>

Followups directed to: comp.lang.python

Show all headers | View raw


Kristofer Tengström wrote:

> Thanks everyone, moving the declaration to the class's __init__ method
> did the trick. Now there's just one little problem left. I'm trying to
> create a list that holds the parents for each instance in the
> hierarchy. This is what my code looks like now:
> 
> -----------------------------------------
> 
> class A:
>     def __init__(self, parents=None):
>         self.sub = dict()
>         if parents:

You should explicitly test for None here; otherwise in a call like

ancestors = []
a = A(anchestors)

the list passed as an argument will not be used, which makes fore confusing 
behaviour.

>             self.parents = parents
>         else:
>             self.parents = []
>     def sub_add(self, cls):
>         hierarchy = self.parents
>         hierarchy.append(self)

Here you are adding self to the parents (that should be called ancestors) 
and pass it on to cls(...). Then -- because it's non-empty -- it will be 
used by the child, too, and you end up with a single parents list.

>         obj = cls(hierarchy)
>         self.sub[obj.id] = obj

While the minimal fix is to pass a copy

def sub_add(self, cls):
    obj = cls(self.parents + [self])
    self.sub[obj.id] = obj

I suggest that you modify your node class to keep track only of the direct 
parent instead of all ancestors. That makes the implementation more robust 
when you move a node to another parent.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need help with simple OOP Python question Kristofer Tengström <krille012@gmail.com> - 2011-09-04 23:47 -0700
  Re: Need help with simple OOP Python question Stephen Hansen <me+list/python@ixokai.io> - 2011-09-05 00:07 -0700
  Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 09:10 +0200
  Re: Need help with simple OOP Python question Ben Finney <ben+python@benfinney.id.au> - 2011-09-05 17:26 +1000
    Re: Need help with simple OOP Python question Kristofer Tengström <krille012@gmail.com> - 2011-09-05 06:15 -0700
      Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 16:43 +0200
        Re: Need help with simple OOP Python question Jon Clements <joncle@googlemail.com> - 2011-09-05 07:59 -0700
          Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 17:28 +0200
      Re: Need help with simple OOP Python question Terry Reedy <tjreedy@udel.edu> - 2011-09-05 13:38 -0400
        Re: Need help with simple OOP Python question Piet van Oostrum <piet@vanoostrum.org> - 2011-09-08 12:53 +0200

csiph-web