Knowee
Questions
Features
Study Tools

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.

...expand
🧐 Not the exact question you are looking for?Go ask a question

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 of C1. So when f is called, it's C1.f. Inside C1.f, self.g() is called, which is C1.g.

  • For obj2, it's an instance of C2 which is a subclass of C1. So when f is called, it's C2.f. Inside C2.f, self.g() is called, but C2 doesn't have a g method, so it looks up to its parent class C1, thus it's C1.g.

  • For obj3, it's an instance of C3 which is a subclass of C1. So when f is called, it's C1.f because C3 doesn't override f. Inside C1.f, self.g() is called, and C3 does have a g method, so it's C3.g.

  • For obj4, it's an instance of C4 which is a subclass of C3 and C1. So when f is called, it's C4.f. Inside C4.f, self.g() is called, and C4 doesn't have a g method, so it looks up to its parent class C3, thus it's C3.g.

This problem has been solved

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.*

1/3

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.