Classes, Inheritance - Stupid lazy question

Michael Hudson mwh21 at cam.ac.uk
Wed Apr 12 11:30:57 CEST 2000


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)

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