inheritance

Ian Kelly ian.g.kelly at gmail.com
Thu Apr 5 13:12:26 EDT 2012


On Thu, Apr 5, 2012 at 10:50 AM, yag <yagnesh at live.com> wrote:
> three classes A,B,C and instance x.
>
> now how can I call methods foo in class A and B using 'x' instance. (I hope I
> could pronounce the terminology correct)

Do you mean that you want C.foo to call B.foo, and B.foo to call
A.foo?  If that is the case, just use super(), as you already do with
the __init__ method.

Or do you want to skip C.foo and call A.foo or B.foo directly?  In
that case, just call it from the specific class you want.  Since you
are dispatching from the class instead of the instance, you will have
to pass the instance in explicitly as the self argument.  For example:

B.foo(x)  # calls B.foo directly with instance x

Cheers,
Ian



More information about the Python-list mailing list