[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What's the purpose the hook method showing in a class definition?

Hi Jach,

On 20/10/2019 09:34, jfong at wrote:
> What puzzles me is how a parent's method foo() can find its child's method goo(), no matter it was overwrote or not? MRO won't explain this and I can't find document about it also:-(

This is a generalised description - Python may be slightly different.

When foo invokes goo the search for goo starts at the class of the 
object (which is B), not the class of the executing method (i.e not A). 
It then proceeds to look for goo up the class hierarchy - first in B, 
then A then Object.

If that fails the RTS modifies the call, to look for a magic method, and 
starts again at B. When the magic method is found in Object, you get the 
"not found" error. If you implement the magic method in A or B it will 
be run instead.



This email has been checked for viruses by AVG.