Need help with simple OOP Python question

Terry Reedy tjreedy at udel.edu
Mon Sep 5 13:38:08 EDT 2011


On 9/5/2011 9:15 AM, 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:
>              self.parents = parents
>          else:
>              self.parents = []
>      def sub_add(self, cls):
>          hierarchy = self.parents
>          hierarchy.append(self)
>          obj = cls(hierarchy)
>          self.sub[obj.id] = obj

Indexing objects by their internal id is usually useless. Considier 
whether you should be using sets rather than dicts.

-- 
Terry Jan Reedy





More information about the Python-list mailing list