Q: Subclassing dict and __cmp__

Michael Hudson mwh at python.net
Mon Oct 21 08:57:44 EDT 2002


tebeka at cs.bgu.ac.il (Miki Tebeka) writes:

> Hello All,
> 
> Can anyone explain the following? (or point me to a link since my
> googleing didn't help this time :)
> --------------
> Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on
> win32
> Type "copyright", "credits" or "license" for more information.
> IDLE Fork 0.8 -- press F1 for help
> >>> class H(dict):
> 	def __cmp__(self, other):
> 		'''Compare only x's'''
> 		if self['x'] == other['x']:
> 			return 0
> 		return 1

Override __eq__ instead.  You might or might not want to override
__le__, __gt__ and so on too.

Cheers,
M.

-- 
  BUGS   Never use this function.  This function modifies its first
         argument.   The  identity  of  the delimiting character is
         lost.  This function cannot be used on constant strings.
                                    -- the glibc manpage for strtok(3)



More information about the Python-list mailing list