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

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!

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?

[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

1 Like

@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.

7 Likes

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.

1 Like

This is the solution to the excercise fgiven by codeacademy:
class PotatoSalad:
def init(self, potatoes, celery, onions):
self.potatoes = potatoes
self.celery = celery
self.onions = onions

class SpecialPotatoSalad(PotatoSalad):
def init(self, potatoes, celery, onions):
super().init(potatoes, celery, onions)
self.raisins = 40

why raisins are not added to the method init() like that: def init(self, potatoes, celery, onions, raising):
self.raisins = 40

2 Likes

I’d also like to know that.

1 Like

Shouldn’t the method in subclass of the exercise be changed from “def init(self, potatoes, celery, onions):” into “def init(self, potatoes, celery, onions, raisins):” i mean what would the “self.raisins = 40” refer to, if the content of the method in subclass be the same as in the parent class?

1 Like

Good point. The example does not really make much sense either way, but including the raisins in the subclass init() method does work. This code also passes the exercise:

class SpecialPotatoSalad(PotatoSalad):
  def __init__(self,potatoes, celery, onions, raisins = 40):
    super().__init__(potatoes, celery, onions)
    self.raisins = raisins

I don’t understand some things in the example code:

def init(self, basin, nozzle, trash_compactor=None):

What does trash_compactor=None mean? Why is there “=None”? I’ve never seen this in an argument position before.

if trash_compactor:
if trash_compactor what? There is no condition, it seems incomplete.

Did you miss the lesson on default parameters? One would hope it was covered before this unit.

trash_compactor=None

represents an optional argument in the function call. So long as at least two positional arguments are given, the __init__ method will set the third parameter to None by default.

True, but does the code work if you don’t include “raisins” in the constructor for the subclass? Instinctively, I find it weird to declare

self.raisins = 40

without including “raisins” in the constructor.

@byteninja93187, you’re right: it does not work:

class SpecialPotatoSalad(PotatoSalad):
  def __init__(self, potatoes, celery, onions, raisins = 40):
    super().__init__(potatoes, celery, onions)
    self.raisins = raisins
    
sps = SpecialPotatoSalad(3,4,5)
print(sps.raisins)

# Output:
40

Now, comment out the line self.raisins = raisins

class SpecialPotatoSalad(PotatoSalad):
  def __init__(self, potatoes, celery, onions, raisins = 40):
    super().__init__(potatoes, celery, onions)
    # self.raisins = raisins

sps = SpecialPotatoSalad(3,4,5)
print(sps.raisins)

# Output:
Traceback (most recent call last):
  File "script.py", line 13, in <module>
    print(sps.raisins)
AttributeError: 'SpecialPotatoSalad' object has no attribute 'raisins'

Thanks, but I (and I think the previous contributors as well) was actually wondering whether the below would work:

class SpecialPotatoSalad(PotatoSalad):
  def __init__(self, potatoes, celery, onions):
    super().__init__(potatoes, celery, onions)
    self.raisins = raisins

Would you be able to declare the instance variable “self.raisins” without including it as a parameter in the constructor?

To me, it seems impossible but I definitely passed the exercise this way.

Not if you try to actually instantiate an object!

class SpecialPotatoSalad(PotatoSalad):
  def __init__(self, potatoes, celery, onions):
    super().__init__(potatoes, celery, onions)
    self.raisins = raisins
    
sps = SpecialPotatoSalad(3,4,5)
# print(sps.raisins)

# Output:
Traceback (most recent call last):
  File "script.py", line 12, in <module>
    sps = SpecialPotatoSalad(3,4,5)
  File "script.py", line 10, in __init__
    self.raisins = raisins
NameError: name 'raisins' is not defined
1 Like

Thanks, I’ll report it!

… and it doesn’t pass:capture

If you put inself.raisins = 40, you are assigning a value to raisins, but self.raisins = raisins throws a name error.