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!

6 posts were split to a new topic: Lesson text used to use self incorrectly

10 posts were split to a new topic: Where do I need to include raisins?

2 posts were split to a new topic: What is a default parameter?

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(basin, nozzle)
if trash_compactor:
self.trash_compactor = trash_compactor

I am so confused on what the purpose of the “if trash_compactor:” is. Why is there an empty if statement here. I’m not understanding this syntax please help :slight_smile: TY!

The programmer has two choices.

  1. Not defining a trash_compactor attribute if the default value is in effect; or,

  2. Defining the trash_compactor attribute with the value of the parameter, regardless whether default or not.

The above code uses option 1. Were we to opt with no. 2, then we would remove the conditional.

Thank you very much for your reply really appreciate it! :slight_smile:

1 Like

I see now. Ah Yes we defined the variable as None so if it doesn’t have an input we dont’ get a traceback error. I was trying the script

if apple:

print(2)

without and with defining apple. But I see we defined the variable as None to start off so we won’t get traceback :). TY!!

1 Like

Hi all,

Thank you for reading this. I have a quick question regarding using the super() function. Why doesn’t the example exercise use ‘self’ in the method that’s an attribute of super()?

Are you asking why we use super().init(potatoes, celery, onions) rather than super().init(self.potatoes, self.celery, self.onions)? If so, I have the same question.

I think it may be because the object has hasn’t been instantiated at the point of this function call yet but I am unsure

If the parent class has those attributes and the subclass has no need to override them, then all we’re doing with super() is instantiating those attributes in the parent class. They are still accessible to the subclass instance.

1 Like

Got it, that makes sense. Thanks for the help

1 Like