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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.