dictionary interface
Antoon Pardon
apardon at forel.vub.ac.be
Wed Oct 5 06:04:20 EDT 2005
Op 2005-10-05, Steve Holden schreef <steve at holdenweb.com>:
> Antoon Pardon wrote:
>>
>> I have been searching some more and finally stumbled on this:
>>
>> http://docs.python.org/ref/comparisons.html
>>
>> Mappings (dictionaries) compare equal if and only if their sorted
>> (key, value) lists compare equal. Outcomes other than equality are
>> resolved consistently, but are not otherwise defined.
>>
>> This seems to imply that the specific method to sort the dictionaries
>> is unimported (as long as it is a total ordering). So I can use whatever
>> method I want as long as it is achieves this.
>>
>> But that is contradicted by the unittest. If you have a unittest for
>> comparing dictionaries, that means comparing dictionaries has a
>> testable characteristic and thus is further defined.
>>
>> So I don't need a full implementation of dictionary comparison,
>> I need to know in how far such a comparison is defined and
>> what I can choose.
>>
> The dict unit tests are probably trying to ensure that the dictionary
> ordering doesn't change from version to version, which is probably a
> good idea in case someone (foolishly?) deciess to rely on it.
I doubt that. Just to check I tried the following:
class Tree:
def __lt__(self, term):
return len(self) < len(term)
And the test passed.
> I can't help wondering, though, under what conditions it actually makes
> sense to compare two dictionaries for anything other than equality.
Yes that is part of the problem, because I can't think of such a
condition it is hard to think of what extra constraints could be
usefull here.
Anyway, I have searched the source of the test for all testing
with regards to < and after some browsing back and fore it seems
it all boils down to the following two tests.
self.assert_(not {} < {})
self.assert_(not {1: 2} < {1L: 2L})
--
Antoon Pardon
More information about the Python-list
mailing list