FAQ: Learn Python: Inheritance and Polymorphism - Review


#1

This community-built FAQ covers the “Review” 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 Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Hi, I just finished the section on polymorphism, and here is what I came up with in order to make the SortedList to be sorted at initialization (spoilers, if you want to solve this bonus yourself, don’t look):

Code
class SortedList(list):
  def __init__(self, values):
    super().__init__(self)
    self.values = values
    self.values.sort()
  
  def append(self, value):
    super().append(value)
    self.sort()

  def __repr__(self):
    return str(self.values)

a = SortedList([3, 45, 1234, 21])

print(a)

And it works, but it seems that it forces me to keep the values outside of the parent class, that is, it seems I can’t make the super().__init__() function initialize the values I receive for me, I tried passing them directly to it, but it said that it only accepts one argument: self.

And this of course then forces me to overwrite __repr__(), because otherwise when I try to print my SortedList it prints an empty list (since the values are not accessible from the parent class).

So I would just like to know if there’s something I’m doing wrong/not optimized or if it’s how it’s supposed to work? In a way it would also make sense to work like that, because otherwise I don’t know how you would access the values in the subclass (maybe from a return from super() ?)

Thanks to anyone who takes the time to answer me.


#3

H, @arjofocolovi. This my personal opinion and I’m no professional expert. I think your code does not exactly correspond to what the exercise asks for, which is that SortedList should be a child class implementation of List. This means that the sorted list is the class object itself. But in your case, the sorted list is the not the class object itself, but rather an instance variable defined inside the class, of type list and named values.

Anyway, here is my code. Hope it is what the exercise requires and hope it helps you.

My Code
class SortedList(list):
  
  def __init__(self, lst):
    super().__init__(lst)
    self.sort()
  
  def append(self, value):
    super().append(value)
    self.sort()
 

new_list = SortedList([4, 1, 5])
print(new_list)
new_list.append(0)
print(new_list)


#4

Indeed, I discovered that later on during the last project, my mistake was to try to pass self when calling super() on __init__, which you shouldn’t do, otherwise you run out of arguments (and you don’t even give the proper arguments anyway). But thanks for your answer, I forgot to update this thread.