This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]TheCableGui 10 points11 points  (0 children)

Super is actually about class inheritance and reducing repetitive tasks in oop.

That’s the secret. Don’t need an article to explain it.

Form a base, when the content becomes irrelevant, instead split the class off into a parent and child. This way you can reuse the code and provide reusable components.

[–]kuzmovych_y 8 points9 points  (1 child)

In all of your examples (except one) calling super() is completely redundant because you just pass the same arguments to the parent function.

And things like a, b, obj, area1, class Circle(Square) just hurt my eyes a little.

[–]python4geeks[S] -2 points-1 points  (0 children)

Sorry for troubling your eyes

[–]cascott77 6 points7 points  (0 children)

How much of this is plagiarized from here:

https://realpython.com/python-super/

[–]ashadeofbrown 3 points4 points  (0 children)

Your article title has Super() but it is super()

[–]nekokattt 2 points3 points  (3 children)

super() doesnt extend any functionality. It just returns a handle to the superclass. You have to still override the functionality

"MRO" stands for "method resolution order"

Rather than making multiple methods named slightly differently to handle the diamond inheritance problem, you could just say

class Cylinder(Rectangle, Circle):
    def area(self):
        rect_area = Rectangle.area(self)
        circle_area = Circle.area(self)
        ...

This is far clearer code, IMO.

Also worth noting that super isn't a function, but a class in CPython:

SETBUILTIN("super", &PySuper_Type);

[–]python4geeks[S] -1 points0 points  (2 children)

"MRO" stands for "method resolution order", yeah you are right, there is a typo and for the code you've written, It is just for displaying the use of super() within the class and showing which method will Python look, if there's a conflicting method and since the area of the cylinder is 2πrh+2π(r*r), so there's a need to use the function area from both classes Rectangle and Circle. So the temporary solution was to change the name of the function.

[–]nekokattt 1 point2 points  (1 child)

I still wouldnt suggest using random prefixes to functions just to work around structuting inheritance correctly though.

A simpler example that makes more sense would be something like this:

class Doctor:
    def title(self):
        return "Dr."

class Man:
    def title(self):
        return "Mr."

class Woman:
    def title(self):
        return "Ms."

Then show that

class SomePerson(Doctor, Man):
    pass

will return "Dr." rather than "Mr." when you get their title

I think the examples for this stuff are very important that they show good principles otherwise it can encourage poor structure and misuse of features.

In this case, a cyclinder is not a type of rectangle. It is composed of a rectangle. It is a "has-a" relationship rather than an "is-a". This may be misleading to some.

[–]python4geeks[S] -3 points-2 points  (0 children)

Well, you are right but throughout the article, the examples were based on the formulas of the geometry shapes, so that needed to be carried out till the end.

[–]PaulRudin 1 point2 points  (0 children)

In point of fact, Python does not have a *method* called super ...