Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBaseA?

The Music Guy musicguy at alphaios.net
Mon Sep 7 07:45:21 CEST 2009


On Sat, Sep 5, 2009 at 8:41 PM, Carl Banks<pavlovevidence at gmail.com> wrote:
> Out of curiosity, did you try this and are reporting that it resulted
> in an AttributeError, or did you merely deduce that it would raise
> AttributeError based on your knowledge of Python's inheritance?
>
> I ask this rhetorically.  I know that you didn't try it (or that you
> tried it and made a mistake) because if you had tried it (and not made
> a mistake) you would have seen that it works exactly as you want it
> to.
>
>
> Carl Banks
> --
> http://mail.python.org/mailman/listinfo/python-list
>

This code causes  an error:

#!/usr/bin/python

def main():
    foox = FooX()
    fooy = FooY()
    fooz = FooZ()

    #foox.method_x("I", "AM", "X")
    print
    fooy.method_x("ESTOY", "Y", "!")
    print
    fooz.method_x(100, 200, 300)


class MyMixin(object):

    def method_x(self, a, b, c):
        super(MyMixin, self).method_x(a, b, c)
        print "MyMixin.method_x(%s, %s, %s, %s)" % (repr(self),
repr(a), repr(b), repr(c))

class BaseA(object):

    def method_x(self, a, b, c):
        super(BaseA, self).method_x(a, b, c)
        print "BaseA.method_x(%s, %s, %s, %s)" % (repr(self), repr(a),
repr(b), repr(c))

class BaseB(object):

    pass

class BaseC(object):

    def method_x(self, a, b, c):
        super(BaseC, self).method_x(a, b, c)
        print "BaseC.method_x(%s, %s, %s, %s)" % (repr(self), repr(a),
repr(b), repr(c))

class FooX(BaseA, MyMixin):

    def method_x(self, a, b, c):
        super(FooX, self).method_x(a, b, c)
        print "FooX.method_x(%s, %s, %s, %s)" % (repr(self), repr(a),
repr(b), repr(c))

class FooY(BaseB, MyMixin):

    pass

class FooZ(BaseC, MyMixin):

    def method_x(self, a, b, c):
        super(FooZ, self).method_x(a, b, c)
        print "FooZ.method_x(%s, %s, %s, %s)" % (repr(self), repr(a),
repr(b), repr(c))

if __name__ == '__main__':
    main()


Traceback (most recent call last):
  File "foo.py", line 54, in <module>
    main()
  File "foo.py", line 10, in main
    fooy.method_x("ESTOY", "Y", "!")
  File "foo.py", line 18, in method_x
    super(MyMixin, self).method_x(a, b, c)
AttributeError: 'super' object has no attribute 'method_x'



More information about the Python-list mailing list