R
R
reus2017-02-13 17:47:21
Django
reus, 2017-02-13 17:47:21

Return overridden method?

In general, such a situation arose.
In one of the classes (or rather in the model), some wonderful person overridden the standard method and now it does not work. In other words, I need to inherit class 3 - everything from class 2, and the desired method from class 1. How to do this?
As I understand it, this is supposedly done with the help of super...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2017-02-13
@Vindicar

Override the desired method yourself? For one or two methods, it may be fine, provided that you are sure that the methods of Class1 will work correctly with instances of your class.

def __methodname__(self, *args, **kwargs):
    return Class1.__methodname__(self, *args, **kwargs)

This, of course, is a hack... but on the other hand, you are explicitly setting the desired behavior from a common ancestor, so there are no new implicit dependencies in theory.

A
Alexander, 2017-02-13
@sanya84

Try this option here:

class X:
    def __init__(self):
        print("Я класс X у меня есть метод:")
        self.x()
    def x(self):
        print("self.x")

class Y:
    def __init__(self):
        print("Я класс Y у меня есть метод:")
        self.y()
    def y(self):
        print("self.y")    

class Z(X,Y):
    def __init__(self):
        print("Я класс Z у меня есть методы:")
        
        self.z()
        self.y()
        self.x()
        
    def z(self):
        print("self.z")


if __name__=="__main__":
    z=Z()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question