Problem with object-in-object and common superclass

Dirk-Ulrich Heise hei at adtranzsig.de
Mon Sep 25 09:33:17 EDT 2000


<ikaran at my-deja.com> schrieb im Newsbeitrag
news:8qn5tj$ucq$1 at nnrp1.deja.com...

I'll "patch" your code, hope you get along with this:

> class Container :

omit this line, it creates a class static variable. not what you want.

>   stuff = []

add this :
  def __init__():
     # a constructor for the base class
     self.stuff = []
          # now it's an instance variable
          # but remember : you gotta call the base class constructor
explicitly
          # from the daughter classes!

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

add this to call the base class constructor:
         Container.__init__(self)

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

add this to call the base class constructor:
         Container.__init__(self)

>     self.stuff.append(Pod())
>
> sprout = Plant()
> sprout.show()

HTH.
--
Dipl.Inform. Dirk-Ulrich Heise
hei at adtranzsig.de
dheise at debitel.net






More information about the Python-list mailing list