Comparing dictionaries, is this valid Python?
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? -- François Pinard http://pinard.progiciels-bpi.ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing dictionaries, is this valid Python?
[François Pinard] ... > 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? Yes, look here : it has the meaning you expect, provided that by "same" you mean "compare equal" (and not, e.g, "is"). See the Language Reference Manual, section 5.9 "Comparisons", for more, and footnote 5.5 there for a bit of history. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing dictionaries, is this valid Python?
François Pinard wrote: > 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). I'm not checking the 2.3.5 version, but that latest one is fairly clear on what comparisons on mappings do: http://docs.python.org/ref/comparisons.html The same behaviour applied to 2.3.5 though, I believe, so maybe that's sufficient for you. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing dictionaries, is this valid Python?
[Peter Hansen] >> it only says "Comparison operations are supported by all objects" >> [...] >I'm not checking the 2.3.5 version, but that latest one is fairly clear >on what comparisons on mappings do: >http://docs.python.org/ref/comparisons.html Yes, indeed. Thanks a lot! -- François Pinard http://pinard.progiciels-bpi.ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing dictionaries, is this valid Python?
François Pinard <[EMAIL PROTECTED]> 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). http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list