Understanding Python super() with __init__() methods

+1 vote
I'm trying to understand the use of super(). From the looks of it, both child classes can be created, just fine.
I'm curious to know about the actual difference between the following 2 child classes.
class Base(object):
    def __init__(self):
    print "Base created"
class ChildA(Base):
    def __init__(self):
    Base.__init__(self)
class ChildB(Base):
    def __init__(self):
    super(ChildB, self).__init__() 
ChildA() 
ChildB()
Aug 28, 2018 in Python by bug_seeker
• 14,970 points
67 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

It's been noted that in Python 3.0+ you can use

super().__init__()

to make your call, which is concise and does not require you to reference the parent OR class names explicitly, which can be handy. I just want to add that for Python 2.7 or under, it is possible to get this name-insensitive behaviour by writing self.__class__ instead of the class name, i.e.

super(self.__class__, self).__init__()

HOWEVER, this breaks calls to super for any classes that inherit from your class, where self.__class__ could return a child class. For example:

class Polygon(object): 
    def __init__(self, id): 
        self.id = id 
class Rectangle(Polygon): 
    def __init__(self, id, width, height): 
        super(self.__class__, self).__init__(id) 
        self.shape = (width, height) 
class Square(Rectangle): 
    pass

Here I have a class Square, which is a sub-class of Rectangle. Say I don't want to write a separate constructor for Square because the constructor for Rectangle is good enough, but for whatever reason I want to implement a Square so I can reimplement some other method.

answered Aug 28, 2018 by Priyaj
• 56,120 points

Related Questions In Python

0 votes
1 answer

How to perform web scraping with python?

Hey, there are various libraries used in ...READ MORE

answered Apr 20, 2018 in Python by aayushi
• 750 points
166 views
+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 65,850 points
1,918 views
0 votes
1 answer

Difference between append vs. extend list methods in Python

append: Appends object at end. x = [1, ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
106 views
0 votes
1 answer

Create an empty list in python with certain size

Try this instead: lst = [None] * 10 The ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 14,970 points
70 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
23 views
+4 votes
6 answers
+1 vote
2 answers

Static methods in Python?

Static methods are used when we need ...READ MORE

answered Aug 4, 2018 in Python by Ronaldobessmin
• 160 points

edited Aug 4, 2018 by Vardhan 44 views
+1 vote
1 answer

Understanding Pickling in Python

While others have pointed to the Python ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 56,120 points
13 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.