FAQ: Learn Python: Inheritance and Polymorphism - Super()


#1

This community-built FAQ covers the “Super()” exercise from the lesson “Learn Python: Inheritance and Polymorphism”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Super()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

the syntax in the lesson for super is the following:

class Sink:
def init(self, basin, nozzle):
self.basin = basin
self.nozzle = nozzle

class KitchenSink(Sink):
def init(self, basin, nozzle, trash_compactor=None):
super().init(self, basin, nozzle)
if trash_compactor:
self.trash_compactor = trash_compactor

and again points out the syntax again:

super().init(self, basin, nozzle)

However, I was surprised to see “self” in the super() syntax from other videos I watched about it. When I followed the syntax the error I got was:

Did you call super().__init__(potatoes, celery, onions) ?

Without the self as a part of it. So is there a reason or is this an error?


#4

[quote=“monochromaticmau, post:2, topic:373200”]
However, I was surprised to see “self” in the super() syntax from other videos I watched about it.

came here to ask the same thing


#5

@monochromaticmau @gstek
I guess, there is something wrong with the instructions. As, I tried running code which was in instructions as an example, I got same error.
So, after that I checked out the syntax of super() in python official documentation, and found out -
1) Syntax of super() in python 3 is
class ParentClass():
def __ init __(self, a, b):
pass

class ChildClass(ParentClass):
def __ init __ (self, a, b, c, d):
super(). __ init __ (a, b)
self.c = c
self.d = d

2) Syntax of super() in python 2 is
class ParentClass():
def __ init __(self, a, b):
pass

class ChildClass(ParentClass):
def __ init __ (self, a, b, c, d):
super(ChildClass, self). __ init __(a, b)
self.c = c
self.d = d

Follow 1st syntax, you will not get any error.
Hope these helps.


#6

Thanks for verifying that @abhijeet_shettiwar, I didn’t realize it was a Python 2 v Python 3 issues. Hope they fix that or at least make mention of it in the instructions.