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,510 points
4,129 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
• 58,020 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
1,798 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
• 69,220 points
10,555 views
+1 vote
3 answers

Difference between append vs. extend list methods in Python

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

answered Aug 21, 2019 in Python by germyrinn
• 240 points
96,840 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,510 points
28,127 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
+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 972 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
• 58,020 points
938 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP