[Python-Dev] Error in Python Tutorial on Multiple Inheritence

Armin Rigo arigo at tunes.org
Thu Aug 12 11:53:13 CEST 2004


Hello,

On Wed, Aug 11, 2004 at 10:41:42PM -0700, Josiah Carlson wrote:
> Perhaps the following example (or a variant) should be included in the
> tutorial as an example.

Including this example would be confusing for two reasons.

One is that it doesn't answer the original concern, which was not about class
attributes but instance attributes, e.g.:

class point:
  def move_to_the_right(self):
    x, y = self.pos
    self.pos = x+1, y

class myfile:
  def seek(self, newpos):
    self.pos = newpos
  def read(self):
    return self.data[self.pos:]

In this example, a class inheriting from both 'point' and 'myfile' will be in
trouble because the same instance attribute 'pos' is used with different
meanings by the methods of the base classes.

Another concern with your example is that some of the behavior it relies upon
is specific to old-style classes (which might be dropped at some time long in
the future).  Try to do the same with new-style classes:

class a(object):    # forces new-style classes
  pass

class b(a):
  pass

class d(a,b):
TypeError: Cannot create a consistent method resultion
order (MRO) for bases b, a

A saner example along those lines would be something like:

class a:
  pass

class b(a):
  x = 1

class c(a):
  x = 2

class d(b,c):
  pass

class e(c,b):
  pass

d().x   # 1
e().x   # 2


A bientôt,

Armin.


More information about the Python-Dev mailing list