Closed mostafa-abdullah closed 7 years ago
What do you mean with "dynamically bound methods"? If they are the methods added to instances at runtime, then we don't support that right?
Look at this code:
class A:
def foo(self): print("A")
class B(A):
pass
class C(A):
def foo(self): print("C")
class X(B,C): pass
The MRO for B is B,A,object and the MRO for X is X,B,C,A,object.
print("MRO:", [x.__name__ for x in B.__mro__])
print("MRO:", [x.__name__ for x in X.__mro__])
Now which version of foo
are you "copying" down to X? Because according to the MRO you would "copy" the foo
from A in B, but then you should not "copy" the newly copied foo
from B in X. Oh, is this the case in which you look at the original class definition (before the copy)?
Yes exactly! I only copy the methods after all the classes are processed, that is every class at first determines which methods it needs to copy, then after that the copying is performed.
So in your example, class X
will select foo
from class C
.
Btw, the example you just wrote is the same one as that in the test file mro.py
.
I think we should also have some way to know that inherited methods do not really exist in the subclasses. Otherwise we might have problems with methods that call other dynamically bound methods: it is possible that these methods are resolved differently in the original program compared to our program where we copy these methods down the subclasses. We do not want this difference.