Should one always add super().__init__() to the __init__?

Ramchandra Apte maniandram01 at gmail.com
Sat Sep 29 23:14:10 EDT 2012


On Saturday, 29 September 2012 22:47:20 UTC+5:30, Steven D'Aprano  wrote:
> On Sat, 29 Sep 2012 06:27:47 -0700, Ramchandra Apte wrote:
> 
> 
> 
> > Should one always add super().__init__() to the __init__? The reason for
> 
> > this is the possibility of changing base classes (and forgetting to
> 
> > update the __init__).
> 
> 
> 
> No. Only add code that works and that you need. Arbitrarily adding calls 
> 
> to the superclasses "just in case" may not work:
> 
> 
> 
> 
> 
> 
> 
> py> class Spam(object):
> 
> ...     def __init__(self, x):
> 
> ...             self.x = x
> 
> ...             super(Spam, self).__init__(x)
> 
> ...
> 
> py> x = Spam(1)
> 
> Traceback (most recent call last):
> 
>   File "<stdin>", line 1, in <module>
> 
>   File "<stdin>", line 4, in __init__
> 
> TypeError: object.__init__() takes no parameters
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

I forgot something:
I meant super().__init__() or similar



More information about the Python-list mailing list