extender method

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Jul 26 13:17:17 EDT 2006


"Chris Lambacher" <chris at kateandchris.net> wrote in message
news:mailman.8573.1153932203.27775.python-list at python.org...
> On Wed, Jul 26, 2006 at 09:21:10AM -0700, davehowey at f2s.com wrote:
> > 'Learning Python' by Lutz and Ascher (excellent book by the way)
> > explains that a subclass can call its superclass constructor as
> > follows:
> >
> > class Super:
> >    def method(self):
> >    # do stuff
> >
> > class Extender(Super):
> >    def method(self):
> >    Super.method(self)   # call the method in super
> >    # do more stuff - additional stuff here
> >

With new-style classes (where Super inherits from object), I think the
preferred style is now:

  super(Extender,self).__init__(**kwargs)

Instead of

  Super.__init__(self,**kwargs)


class Super(object):
    def __init__(self, **kargs):
        print kargs
        self.data = kargs

class Extender(Super):
    def __init__(self, **kargs):
        #~ Super.__init__(self, **kargs)   # call the constructor method in
Super
        super(Extender,self).__init__(**kargs)

e = Extender(a=123)

prints:
{'a': 123}

-- Paul





More information about the Python-list mailing list