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

Chris Angelico rosuav at gmail.com
Sun Oct 20 06:32:13 EDT 2019


On Sun, Oct 20, 2019 at 9:06 PM Ian Hobson <hobson42 at gmail.com> wrote:
>
> 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.
>

Yeah, pretty much. In a simple case of linear inheritance, that's how
it works in Python. There are complexities with multiple inheritance
and different languages will do things differently, but that doesn't
affect this example.

ChrisA



More information about the Python-list mailing list