Subclassing dict and __cmp__

Terry Reedy tjreedy at udel.edu
Sun Oct 20 10:51:21 EDT 2002


"Miki Tebeka" <tebeka at cs.bgu.ac.il> wrote in message
news:33803989.0210192240.6dced01b at posting.google.com...
>Can anyone explain the following?

> >>> class H(dict):
> def __cmp__(self, other):
> '''Compare only x's'''
> if self['x'] == other['x']:
> return 0
> return 1

> >>> h1 = H()
> >>> h2 = H()
> >>> h1 == h2
> 1

Your __cmp__ does not get called.  Not all methods of builtin types
are overridable.  Guido has said that subclassing types is better for
extending rather than replacing behavior.  Not sure if all rules yet
documented anywhere yet.

Terry J. Reedy





More information about the Python-list mailing list