Inherited problems. - Python

Someone had the same problem on this site, but the answer did not concern the part with which I have problems.
Inheritance - method invocation

Consider the following class definitions.

class C1(object):
    def f(self):
        return 2*self.g()

    def g(self):
        return 2

class C2(C1):
    def f(self):
        return 3*self.g()


class C3(C1):
    def g(self):
        return 5

class C4(C3):
    def f(self):
        return 7*self.g()

obj1 = C1()
obj2 = C2()
obj3 = C3()
obj4 = C4()

For this task, you should consider what methods are called when the method is called f. So, for example, when called obj1.f(), a method f C1is called that calls the method g C1. This can be represented as a β€œcall list” form

['C1.f', 'C1.g'] 

, " " obj2.f() obj2_calls, " " obj3.f() obj3_calls " " ' obj4.f() obj4_calls.

, obj2_calls = ['C2.f', 'C1.g']
, . , C3.f, ['C1.f'], , , .

, ,

+5
2

(MRO) . (. )

, C3.f , python f MRO, f. (C1). (C1.f) self.g(). self C3, , , self.g C3.g, g MRO. , C1.g, :

class C1(object):
    def f(self):
        return 2*C1.g(self)
    def g(self):
        return 5

, . , , - .

+3

obj3_calls ['C1.f', 'C3.g']. , C3.f, C1. , , self.g, C3.g -, , C1.

+1

All Articles