[Python-ideas] Thread.__init__ should call super()

Neil Girdhar mistersheik at gmail.com
Sat Oct 28 03:14:31 EDT 2017

On Friday, October 27, 2017 at 8:05:17 PM UTC-4, Steven D'Aprano wrote:
> On Fri, Oct 27, 2017 at 01:59:01PM -0700, Ilya Kulakov wrote: 
> > Since one of the legit use-cases of using the Thread class is 
> subclassing, 
> > I think it's __init__ should call super() to support cooperative 
> inheritance. 
> > 
> > Or perhaps there is a good reason for not doing so? 
> Are you talking about threading.Thread or some other Thread? 
> If you are talking about threading.Thread, its only superclass is 
> object, so why bother calling super().__init__? 

The way cooperative multiple inheritance works is that if someone defines

class SomeClass(Thread):

     def __init__(self, **kwargs):

they expect this will initialize the base class Thread as desired.

Now, if they add another base class:

class SomeBase:

    def __init__(self, base_x):
        self.base_x = base_x

then they need to pass up the arguments:

class SomeClass(SomeBase, Thread):

     def __init__(self, **kwargs):

Unfortunately, if the order of base classes is reversed, this no longer 
works because Thread doesn't call super:

class SomeClass(Thread, SomeBase):

     def __init__(self, **kwargs):
           super().__init(**kwargs)  # SomeBase is not initialized!

As things get more complicated it's not always possible to ensure that 
Thread is the last class in the inheritance, e.g., if there are two classes 
like Thread that don't call super.

> To be successful, it would need to strip out all the parameters and just 
> call: 
>     super().__init__() 
> with no args, as object.__init__() takes no parameters. And that does 
> nothing, so what's the point? 
> I'm afraid I don't see why you think that threading.Thread needs to call 
> super. Can you explain? 
> -- 
> Steve 
> _______________________________________________ 
> Python-ideas mailing list 
> Python... at python.org <javascript:> 
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171028/cc3103a9/attachment.html>

More information about the Python-ideas mailing list