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
• 15,360 points
538 views

1 answer to this question.

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,940 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
315 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
• 67,660 points
3,412 views
+1 vote
2 answers

Difference between append vs. extend list methods in Python

Python append() method adds an element to ...READ MORE

answered Aug 21 in Python by germyrinn
• 180 points
258 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
• 15,360 points
927 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 350 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 65 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,940 points
29 views