Multiple hierarchie and method overloading

Ben Cartwright bencvt at gmail.com
Mon Apr 24 22:24:11 EDT 2006


Philippe Martin wrote:
> I have something like this:
>
> Class A:
>         def A_Func(self, p_param):
>          .....
> Class B:
>         def A_Func(self):
>          .....
>
> Class C (A,B):
>         A.__init__(self)
>         B.__init__(self)
>
> .....
>
>         self.A_Func() #HERE I GET AN EXCEPTION "... takes at least 2 arguments (1
> given).
>
>
> I renamed A_Func(self) to fix that ... but is there a cleaner way around ?

When using multiple inheritence, the order of the base classes matters!
 E.g.:

  class A(object):
      def f(self):
          print 'in A.f()'
  class B(object):
      def f(self):
          print 'in B.f()'
  class X(A, B):
      pass
  class Y(B, A):
      pass

  >>> x = X()
  >>> x.f()
  in A.f()
  >>> y = Y()
  >>> y.f()
  in B.f()

If you want to call B.f() instead of A.f() for an X instance, you can
either rename B.f() like you've done, or do this:

  >>> B.f(x)
  in B.f()

--Ben




More information about the Python-list mailing list