__eq__ on a dict

Aaron Bingham bingham at cenix-bioscience.com
Mon Jul 11 07:54:05 EDT 2005


"Steven D'Aprano" <steve at REMOVETHIScyber.com.au> writes:

> On Mon, 11 Jul 2005 12:42:55 +0200, Neil Benn wrote:
>
>> Hello,
>> 
>>             I can't find the docs for __eq__ on a dict and I can't find 
>> a description on what the eq does (strangely it does implement > and < 
>> but I have no idea what that does).  Does anyone know (definitively) 
>> what the __eq__, __gt__, __lt__ methods do.

[snip]

> For any two objects x and y, when you call 
>
> x == y
>
> Python calls x.__eq__(y). That includes dicts:
>
> py> dictA = {0: "spam"}
> py> dictB = {0: "sp" + "am"}
> py> dictC = {1: "ham"}
> py> 
> py> dictA == dictB
> True
> py> dictA.__eq__(dictB)   # same as dictA == dictB
> True
> py> dictB == dictC   # calls dictB.__eq__(dictC)
> False
>
> Two dicts are equal if they have the same keys and the same values.

That is what I would expect, but where is that documented?  Also,
where is the behavior of the much less obvious dictionary methods
__ge__, __gt__, __le__, __lt__, and __cmp__ methods documented?

> In general, you should not call __eq__ directly, but use the == operator
> instead.

That is clear enough, the OP was seeking information about the
behavior of these operators when used with dictionaries.

Thanks,

-- 
--------------------------------------------------------------------
Aaron Bingham
Senior Software Engineer
Cenix BioScience GmbH
--------------------------------------------------------------------




More information about the Python-list mailing list