Re: __eq__ on a dict
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: __eq__ on a dict

From: Aaron Bingham <bingham@cenix-bioscience.com>
Date: Mon Jul 11 2005 - 13:54:05 CEST

"Steven D'Aprano" <steve@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
--------------------------------------------------------------------
Received on Thu Sep 29 16:52:35 2005