Comparing dictionaries, is this valid Python?

Mike Meyer mwm at mired.org
Tue Dec 13 21:02:10 EST 2005


François Pinard <pinard at iro.umontreal.ca> writes:
> Would someone know where I could find a confirmation that comparing
> dictionaries with `==' has the meaning one would expect (even this is
> debatable!), that is, same set of keys, and for each key, same values?

It may not exist, so you'll have to go look at the source. That sure
looks like it does what you expect.

However, you should know that *any* to objects in python can be
compared for equality. The default behavior is to check to see if they
are "the same" object. If some class or type does anything else, it'll
have code to do the comparison. You can check the latter behavior
yourself pretty easily:

>>> a = dict(a = 1)
>>> b = dict(a = 1)
>>> a is b
False
>>> a == b
True

So dictionaries have their own comparison code. I can't think of much
else it could be but what you suggest.

Now, what asking if one dictionary is less than another means, there
you're on your own (but that's in the source as well).

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list