1

Here is an example model of what I have in my python code

class BasicActivity:
    def run(self):
        print "basic run"

    def jump(self):
        print "basic jump"

class ChickenStyleActivity(BasicActivity):
    def run(self):
        print "run like chicken"

class BunnyStyleActivity(BasicActivity):
    def jump(self):
        print "jump like bunny"

class ExtraordinaryActivity(ChickenStyleActivity,BunnyStyleActivity):
    pass

d = ExtraordinaryActivity()
d.run()
d.jump()

I was expecting to have both chicken and bunny styles in one class, but surprisingly for me it was chicken run but basic jump. I started reading about method resolution order and now I can see that changing order of classes in multiple inheritance will bring the opposite effect.

The question is - how can I have both Chicken and Bunny styles in one class?

More generally: how to properly inherit several classes which have common base class when method overriding comes into play?

pavel_kazlou
  • 1,995
  • 4
  • 28
  • 34

1 Answers1

4

I think you're using Python 2.x, not Python 3.x.

Therefore BasicActivity needs to inherit from object:

class BasicActivity(object):
    pass

Alex Martelli describes it here: Method Resolution Order (MRO) in new style Python classes

In Python 3 all classes inherit from object

Community
  • 1
  • 1
Dave Halter
  • 15,556
  • 13
  • 76
  • 103