Inheriting from object

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jul 2 15:26:49 EDT 2005


Bengt Richter wrote:
> On Thu, 30 Jun 2005 08:54:31 -0700, Scott David Daniels <Scott.Daniels at Acm.Org> wrote:
>>Or, perhaps:
>>    class foo(object):
>>        def __init__(self, *args, **kwargs):
>>            super(foo, self).__init__(self, *args, **kwargs)
>>            ...
>>
> 
> Doesn't super(foo, self).__init__ return a bound method, so you don't
> need to pass self again? I.e.,
>               super(foo, self).__init__(*args, **kwargs)

Yes, of course (a silly cut-o / paste-o).

> BTW, there's something about referring to type(self) by its not
> always dependably bound (though usually global) name that bothers me.
> 
> I wonder if the above common use of super could be implemented as a property of object,
> so you'd normally inherit it and be able to write
>     self.super.__init__(*args, **kwargs)  # (maybe spell it self.__super__.__init__(...) I suppose)
> 
> I.e., self.__super__ would effectively return the equivalent of
>     super(type(self), self)

The problem with this is:

     class A(object):
         def __init__(self, *args, **kwargs):
             print 'Set A(*%r, **%r)' % (args, kwargs)
     class B(A):
         def __init__(self, *args, **kwargs):
             print 'Set B(*%r, **%r)' % (args, kwargs)
             super(B, self).__init__(*args, **kwargs)
     class C(B):
         def __init__(self, *args, **kwargs):
             print 'Set C(*%r, **%r)' % (args, kwargs)
             super(C, self).__init__(*args, **kwargs)

     class D(A):
         def __init__(self, *args, **kwargs):
             print 'Set D(*%r, **%r)' % (args, kwargs)
             super(type(self), self).__init__(*args, **kwargs)
     class E(D):
         def __init__(self, *args, **kwargs):
             print 'Set E(*%r, **%r)' % (args, kwargs)
             super(type(self), self).__init__(*args, **kwargs)


You'll see the problem when you attempt to create an instance of E.
All of the others work just fine.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list