assymetry between a == b and a.__eq__(b)

Tim Peters tim.peters at gmail.com
Sat Dec 4 11:46:08 EST 2004


[Mel Wilson]
> :) Seems to:
> 
> 
> Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class Eq(object):
> ...     def __eq__(self, other):
> ...             return True
> ...
> >>> class Neq(Eq):
> ...     def __eq__(self, other):
> ...             print "(according to Neq)"
> ...             return False
> ...
> >>> eq,neq=Eq(),Neq()
> >>> eq==neq
> (according to Neq)
> False
> >>> neq==eq
> (according to Neq)
> False
> >>>

See the Python (language, not library) reference manual, section 3.3.8
("Coercion rules"), bullet point starting with:

    Exception to the previous item: if the left operand is an
    instance of a built-in type or a new-style class, and the right
    operand is an instance of a proper subclass of that type or
    class, ...



More information about the Python-list mailing list