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.

1 Like

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!!

2 Likes

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

Can someone tell me please what exactly is wrong with the code above ? This is for the question 2 , of the Super() lesson , on the Inheritance and Polymorphism of the learn Python course. I checked the solution and as far as i can see it is exactly the same code, but maybe i am missing something.

Is is possible to pass a sink object into our KitchenSink definition?

It will treat it as the object it expects to receive.

Hi @mtf thank you for the prompt response and dedication to this community. We defined a KitchenSink class which inherited it’s definition from the Sink class. Could we pass a sink instance into our definition of KitchenSink when creating an instance of kitchen sink? Thus, taking an existing sink object and making it a kitchen sink.

100% totally agree. Diving a little deeper, what defines that expected link?

2 Likes
class KitchenSink(Sink):

All of the attributes of Sink are inherited by KitchenSink by virtue of passing it into the subclass definition. I don’t know enough about the inner workings of Python to explain how this works, but it does

Hi, quick question why is the parameter “trash_compactor” equal to “none”, and what effect would it have if none was removed

Try removing it and see for yourself. What happens?

There was no effect that I could see