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

jfong at ms4.hinet.net jfong at ms4.hinet.net
Sun Oct 20 19:57:54 EDT 2019


Ian Hobson於 2019年10月20日星期日 UTC+8下午6時05分11秒寫道:
> Hi Jach,
> 
> On 20/10/2019 09:34, jfong at ms4.hinet.net 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.
> 
> Regards
> 
> Ian
> 
> -- 
> This email has been checked for viruses by AVG.
> https://www.avg.com

I see. An obj.method will be resolved according to the MRO of the obj's class, no matter where it is located. Thank you.

--Jach



More information about the Python-list mailing list