Classes, Inheritance - Stupid lazy question

Paul Gresham gresham at mediavisual.com
Wed Apr 12 06:18:24 EDT 2000


"Michael Hudson" <mwh21 at cam.ac.uk> wrote in message
news:m3itxnis32.fsf at atrus.jesus.cam.ac.uk...
> Janko Hauser <jhauser at ifm.uni-kiel.de> writes:
>
> > I think this will do it
> >
> > class lazy:
> >     def __init__(self, name):
> >         self.name = name
> >
> >     def fired(self):
> >         print 'You are fired %s' % self.name
> >
> > class paul(lazy):
> >     def __init__(self, name):
> >         self.name = name  # <---- ?????
> >         self.april_pay = 0
> >
> >     def fired(self):
> >         print "Here's a box, collect the things from your desk"
> >         self.__class__.__bases__[0].fired(self)
>
> Nononono; think about what happens if you do this:
>
> class bob(paul):
>    pass
>
> bob("adam").fired()
>
> What *I* think you want is:
>
> class paul(lazy):
>     def __init__(self, name):
>         lazy.__init__(self,name)
>         self.april_pay = 0
>
>     def fired(self):
>         print "Here's a box, collect the things from your desk"
>         lazy.fired(self)
>
> After all, if you're going to inherit from a class, you must have
> access to it, right?
>
> another option is:
>
> class paul(lazy):
>     super = lazy
>     def __init__(self, name):
>         self.super.__init__(self,name)
>         self.april_pay = 0
>
>     def fired(self):
>         print "Here's a box, collect the things from your desk"
>         self.super.fired(self)
>

I like this solution a lot, it makes a lot of sense. Thanks Michael.
I guess somewhere in Python is a defined route back to the super class, on a
better day I may just dig through the code.

Thanks again
>
> Though this has problems with repeated & multiple inheritance.
>
> Cheers,
> M.
>
> --
>   "declare"?  my bogometer indicates that you're really programming
>   in some other language and trying  to force Common Lisp into your
>   mindset.  this won't work.           -- Erik Naggum, comp.lang.lisp





More information about the Python-list mailing list