Problem with object-in-object and common superclass

Alex cut_me_out at hotmail.com
Mon Sep 25 07:44:36 EDT 2000


Your problem is that Container.stuff is a class variable, which as you
noticed is shared between all Container instances.  You need to make it
an instance variable, by defining it in the constructor, or something.
Something like this would work:


class Container :

  def __init__(self):
    self.stuff = []

  def show(self) :
    for thing in self.stuff :
      if isinstance(thing, Container) :
        thing.show()
      else :
        print thing

class Pod(Container) :

  def __init__(self) :
    Container.__init__(self)
    self.stuff.append("pea")

class Plant(Container) :

  def __init__(self) :
    Container.__init__(self)
    self.stuff.append(Pod())

sprout = Plant()
sprout.show()

Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list