frozendict

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 9 15:52:17 CET 2012


Nathan Rice <nathan.alexander.rice at gmail.com> wrote:

> On Thu, Feb 9, 2012 at 5:33 AM, Duncan Booth
><duncan.booth at invalid.invalid> wrote:
>> Nathan Rice <nathan.alexander.rice at gmail.com> wrote:
>>
>>> I put dicts in sets all the time.  I just tuple the items, but that
>>> means you have to re-dict it on the way out to do anything useful
>>> with it.  I am too lazy to write a frozendict or import one, but I
>>> would use it if it was a builtin.
>>>
>> I hope you sort the items before putting them in a tuple, otherwise
>> how d 
> o
>> you handle two identical dicts that return their items in a different
>> order?
> 
> Two dicts created from the same inputs will return items in the same
> arbitrary order.  As long as you don't insert or delete a key you're
> fine.
> 
Two dicts that contain the same keys and values may or may not return them 
in the same order:

>>> dict.fromkeys('ia')
{'i': None, 'a': None}
>>> dict.fromkeys('ai')
{'a': None, 'i': None}

Would your system count those two dicts as the same?

If the sequence in which the keys were added to the dict (and deleted if 
appropriate) is exactly the same then it is likely but still not guaranteed 
that they will have the same order. The only ordering guarantee is that 
within a single dict keys and values will appear in a consistent order so 
long as you don't add/delete keys between calls.


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list