My base code looks like this:
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()
Now my question is the following: I need to write three assignment statements that do the following:
- assign the calling list for
obj2.f()to the variableobj2_calls - assign the calling list for
obj3.f()to the variableobj3_calls - assign the calling list for
obj4.f()to the variableobj4_calls
Calling list being for example, when obj1.f() is called, the f method of C1 is called which calls the g method of C1. This could be represented as a calling list of the form ['C1.f', 'C1.g']
I don’t quite know the proper way to write the assignment statements and I desperately want to help out my friend with her stuff.
If you could just show me how to properly right out the first assignment statement, I’m sure I could figure out the rest.
The key insight is that if a method is not defined for a class, it will default to use the method of a class that the class inherits.
Thus, ask yourself what ‘obj2.f()’ will do.
obj2? It isC2.fmethod defined for classC2? Yes, there is. So the methodC2.fis called.C2.fmethod callsself.g, which means it looks forC2.g. Is there a ‘C2.g’ method? No, so we have to go to the class thatC2inherits from. The lineclass C2(C1)tells us that it is inherited from the classC1, so it will call the methodC1.g.Those are the steps to get the first calling list; the rest are up to you.