Comparing dictionaries, is this valid Python?

  • Thread starter =?iso-8859-1?Q?Fran=E7ois?= Pinard
  • Start date
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

Hi, people.

I noticed today that dictionaries seem to support `==' comparison.
(Retrospectively, it is strange that I never needed it before! :)
Yet, before relying on this, I seeked for confirmation in the Python
manuals, and did not succeed in finding it. In particular:

http://www.python.org/doc/2.3.5/lib/typesmapping.html

is silent on the subject. As for:

http://www.python.org/doc/2.3.5/lib/comparisons.html

it only says "Comparison operations are supported by all objects", which
is a little vague, and no promise that comparisons are meaningful (for
example, one might wonder what would exactly mean the comparison of open
files). The node even says: "Two more operations with the same
syntactic priority, "in" and "not in", are supported only by sequence
types (below).", which suggests that the information might not be fully
up-to-date, at least regarding dictionaries.

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?
 
M

Mike Meyer

François Pinard said:
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:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top