Super().append

In this code

class SortedList(list):
  def append(self, value):
    super().append(value)
    self.sort()

When you say

super().append(value)

What are you appending value too? It looks like your just calling append on nothing. This is python.

Thanks,

Andrej

Is there a particular lesson that this is a part of?
(please link).

Python is object oriented programming language. Inheritance is an important concept to understand. (Parent-child-sibling)
Check this out:
https://appdividend.com/2019/01/22/python-super-function-example-super-method-tutorial/
“Python super() function gives you access to methods in a superclass from the subclass that inherits from it. The super() alone returns a temporary object of the superclass that then allows you to call that superclass’s methods.”

Yes I know what super() is used for but I am confused in my example as to where you are appending “value” when you call the super function from list

I will read through your article too

I think you’re appending value to the class SortedList

I apologize. I jumped in where I probably shouldn’t have.
Let’s see if someone else can help you.

What does super() return if you print it? It’s appending value to that object.

Why not refer to that object directly? Well you could, but writing it this way means you can transpose the code for other situations where you would do the exact same thing.

Now, whether or not one should append the value to that object or what that’s for, that’s a different conversation.

Here’s a very useful resource, recommended by the python documentation: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

Ok, I will read through the article. But before I do I want to ask how would you refer to the object directly using super? Also, I am a little confused by what you mean when you say you can transpose the code for other situations.

Also when I print super() I get the child class. Why is this? I thought super calls to the parent class.

I think I see what you mean by transpose:

class LoggingDict(SomeOtherMapping):            # new base class
    def __setitem__(self, key, value):
        logging.info('Settingto %r' % (key, value))
        super().__setitem__(key, value)         # no change needed
1 Like

A super() print will give you the whole super object reference:
So if a cube is a child of a square, if you call super() within cube you’ll get:

<super: <class 'Cube'>, <Cube object>>

instead of square. But it doesn’t mean that it’s referring to the cube.

To refer to the object directly you just have to type it’s name. I think the first example in the link I referenced talks about this.

As for the line about transposition, I’m talking about the DRY principle (don’t repeat yourself). If you have a code that can be written to apply to multiple situations, it’s better to write it that way than specifically for each situation.

For example, if I wanted to refer to super() in multiple classes, it’s better to use super() than to type the different names of all the super() references in each class. The benefit to do this is that in the future the class changes but has the same structure, the code won’t need to be adjusted. There may be cases where this is not necessary, or suggested, but they don’t come to mind right now.

@dev6112515278 yes that’s right. Python might not be a perfect language. But then again, what language is? I do appreciate that they try to maintain it in tidy shape. And there’s no shortage of knowledgeable people to clarify its more obscure areas.

Ok so here

class SortedList(list):
  def append(self, value):
    super().append(value)
    self.sort()

I could write it like

class SortedList(list):
  def append(self, value):
    list.append(value)
    self.sort()

But I am still confused as to why we are appending value to list and not SortedList where it should be appended.

Usually with append you do this

listIWantToAppendToo.(ThingIWantToAppend)

This is from one of the CC projects?

The best way to figure out is to trace the code and do some unit testing on it. I still have to go back to some of those exercises and break down the algorithms like this.

Are there edge cases that work with one but not the other?? And similar lines of questioning will highlight why certain design decisions are made.

Im not quite sure what your saying.

My main question is where does value get appended too in this code

class SortedList(list):
  def append(self, value):
    list.append(value)
    self.sort()

It seems like we are just calling append without appending to a specific list

This is the exercise: https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/inheritance-and-polymorphism/exercises/review-concepts

It gets appended to <class ‘list’>

If it’s the exercise I’m thinking of (I don’t have pro anymore), it’s modifying the properties of the list class itself. <<< this line is wrong. my memory is bad haha

But in the exercise its supposed to get appended to SortedList. You are supposed to overwrite the append method so it works for SortedList. And they say that the answer is the way I quoted before. Which is confusing me

Ya it is that exercise. But its supposed to get appended to SortedList

" First, we want our new .append() to actually add the item to the list.

Write the code that would get SortedList to behave like a normal list when calling the .append() method."

That’s the answer code they gave you?

Yea they said

class SortedList(list):
  def append(self, value):
    super().append(value)
    self.sort()

Is the answer. Which is why im confused. When it says “self.sort()” its referring to the instance of SortedList. but super().append is not.

“After you’ve appended the new value, sort the list.”

" You can call a parent class method using the super() function like this":

def class_method(self, argument1):
  super().class_method(argument1)

This would make senses to me

class SortedList(list):
  def append(self, value):
    self.super().append(value)
    self.sort()

Because then you are appending the value to self using the append method from list

class SortedList(list):
	def append(self, value):
		super().append(value)
		value.sort()

test= [1,3,2,4]

slist =SortedList()

slist.append(test)

print(slist)

but that gives

output:

[[1, 2, 3, 4]]

which is not good enough

maybe something like this:

class SortedList(list):
	def append(self, value):
		value.sort()
		for i in value:
			super().append(i)

then this works

test= [1,3,2,4]

slist =SortedList()

slist.append(test)

print(slist)

output

[1, 2, 3, 4]

It depends on what you want to do with the class. I don’t know exactly how they want you to use it. But that’s one possible way to play with it (not terribly useful).

So here

class SortedList(list):
  def append(self, value):
    super().append(value)
    self.sort()

Value is not getting appended to SortedList?

Its pretty much calling this method

super().append(value)

But not appending it to anything

The problem in that code is this doesn’t seem to do anything self.sort() (without context)

Is there some other context code around it, or linked file?

But not appending it to anything

Well this is interesting behaviour, it does append to itself here, don’t know if that’s what you want:

slist =SortedList()
slist.append(test)
print(slist)

output

[1, 2, 3, 4]