BackgroundStudy the following class definitions.class C1(): def f(self): return 2*self.g() def g(self): return 2class C2(C1): def f(self): return 3*self.g()class C3(C1): def g(self): return 5class C4(C3): def f(self): return 7*self.g()obj1 = C1()obj2 = C2()obj3 = C3()obj4 = C4()Consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will not always be straightforward.TaskFor each of obj1, obj2, obj3, and obj4, print the methods which are called following a call to objn.f(), where n is the number of the obj, in the format:objn: call1, call2, call3though there may be more or less than three calls.Take care to insert spaces after the colon and the commas.ExamplesYou should print:obj1: C1.f, C1.gand we have already done this for you in the starter code.
Question
BackgroundStudy the following class definitions.class C1(): def f(self): return 2self.g() def g(self): return 2class C2(C1): def f(self): return 3self.g()class C3(C1): def g(self): return 5class C4(C3): def f(self): return 7*self.g()obj1 = C1()obj2 = C2()obj3 = C3()obj4 = C4()Consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will not always be straightforward.TaskFor each of obj1, obj2, obj3, and obj4, print the methods which are called following a call to objn.f(), where n is the number of the obj, in the format:objn: call1, call2, call3though there may be more or less than three calls.Take care to insert spaces after the colon and the commas.ExamplesYou should print:obj1: C1.f, C1.gand we have already done this for you in the starter code.
Solution
Sure, here is the solution in Python:
print("obj1: C1.f, C1.g")
print("obj2: C2.f, C1.g")
print("obj3: C1.f, C3.g")
print("obj4: C4.f, C3.g")
Explanation:
-
For
obj1, it's an instance ofC1. So whenfis called, it'sC1.f. InsideC1.f,self.g()is called, which isC1.g. -
For
obj2, it's an instance ofC2which is a subclass ofC1. So whenfis called, it'sC2.f. InsideC2.f,self.g()is called, butC2doesn't have agmethod, so it looks up to its parent classC1, thus it'sC1.g. -
For
obj3, it's an instance ofC3which is a subclass ofC1. So whenfis called, it'sC1.fbecauseC3doesn't overridef. InsideC1.f,self.g()is called, andC3does have agmethod, so it'sC3.g. -
For
obj4, it's an instance ofC4which is a subclass ofC3andC1. So whenfis called, it'sC4.f. InsideC4.f,self.g()is called, andC4doesn't have agmethod, so it looks up to its parent classC3, thus it'sC3.g.
Similar Questions
class One: def __str__(self): return '1'class Two(One): def __init__(self): super().__init__()class Three(Two): def __init__(self): super().__init__()def main(): object1 = Two() object2 = One() object3 = Three() print(object1, object2, object3)
Which of these method of Object class is used to obtain class of an object at run time?
Which type of method in Python is not bound to either the class or instances and does not receive the class or instance as its first argument?
what will be the output of the following code?class Myclass: x=0 def incre(self): self.x+=1 def disp(self): print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()
One or more method can be declared in a class.*
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.