Re: frozendict (v0.1)

2010-10-09 Thread John Nagle
On 10/7/2010 2:39 PM, kj wrote: Following a suggestion from MRAB, I attempted to implement a frozendict class. That really should be built into the language. "dict" is the last built-in type that doesn't have an immutable form. John Nagle -- http://mail.pyth

Re: frozendict (v0.1)

2010-10-08 Thread Arnaud Delobelle
kj wrote: > In <878w29kxjp@gmail.com> Arnaud Delobelle writes: > > >E.g., try with {1:'a', 1j:'b'} > > I see. Thanks for this clarification. I learned a lot from it. > > I guess that frozenset must have some way of canonicalizing the > order of its elements that is dependent on their Pytho

Re: frozendict (v0.1)

2010-10-08 Thread Steven D'Aprano
On Fri, 08 Oct 2010 14:00:17 +, kj wrote: > In kj writes: > >>At any rate, using your [i.e. Arnaud's] suggestions in this and your >>other post, the current implementation of frozendict stands at: > >>class frozendict(dict): >>for method in ('__delitem__ __setitem__ clear pop popitem'

Re: frozendict (v0.1)

2010-10-08 Thread Steven D'Aprano
On Fri, 08 Oct 2010 12:10:50 +, kj wrote: > In <4cae667c$0$29993$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano > writes: > >>On Fri, 08 Oct 2010 00:23:30 +, kj wrote: > >>Because it's always better to use a well-written, fast, efficient, >>correct, well-tested wheel than to invent

Re: frozendict (v0.1)

2010-10-08 Thread kj
In "Jonas H." writes: >Hope this helps :-) It did! Thanks! For one thing now I see that I was barking up the wrong tree in focusing on a canonical order, when, as the code you posted shows, it is actually not required for hashing. In fact, I'd come to the conclusion that frozensets had a c

Re: frozendict (v0.1)

2010-10-08 Thread kj
In "Jonas H." writes: >On 10/08/2010 02:23 AM, kj wrote: >Here's my implementation suggestion: >class frozendict(dict): > def _immutable_error(self, *args, **kwargs): > raise TypeError("%r object is immutable" % self.__class__.__name__) > __setitem__ = __delitem__ = clear = p

Re: frozendict (v0.1)

2010-10-08 Thread kj
In kj writes: >At any rate, using your [i.e. Arnaud's] suggestions in this and >your other post, the current implementation of frozendict stands >at: >class frozendict(dict): >for method in ('__delitem__ __setitem__ clear pop popitem setdefault ' > 'update').split(): >

Re: frozendict (v0.1)

2010-10-08 Thread Jonas H.
On 10/08/2010 03:27 PM, kj wrote: I tried to understand this by looking at the C source but I gave up after 10 fruitless minutes. (This has been invariably the outcome of all my attempts at finding my way through the Python C source.) It's not you. CPython's code is ... [censored] Anyway, you

Re: frozendict (v0.1)

2010-10-08 Thread kj
In <878w29kxjp@gmail.com> Arnaud Delobelle writes: >E.g., try with {1:'a', 1j:'b'} I see. Thanks for this clarification. I learned a lot from it. I guess that frozenset must have some way of canonicalizing the order of its elements that is dependent on their Python values but not on their

Re: frozendict (v0.1)

2010-10-08 Thread kj
In <4cae667c$0$29993$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano writes: >On Fri, 08 Oct 2010 00:23:30 +, kj wrote: >Because it's always better to use a well-written, fast, efficient, >correct, well-tested wheel than to invent your own slow, incorrect >wheel :) IOW, "don't you wo

Re: frozendict (v0.1)

2010-10-08 Thread Jonas H.
On 10/08/2010 02:23 AM, kj wrote: I imagine that frozenset is better than sorted(tuple(...)) here, but it's not obvious to me why. dicts are unsorted. That means their item-order is undefined. So are sets. If you want a hash that is independent from the order of items, you could ensure the it

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
kj writes: > In <87hbgxlk67@gmail.com> Arnaud Delobelle writes: > >>A simple fix is to use hash(frozenset(self.items())) instead. > > Thanks for pointing out the hash bug. It was an oversight: I meant > to write > > def __hash__(self): > return hash(sorted(tuple(self.items(

Re: frozendict (v0.1)

2010-10-07 Thread Steven D'Aprano
On Fri, 08 Oct 2010 00:23:30 +, kj wrote: > In <87hbgxlk67@gmail.com> Arnaud Delobelle > writes: > >>A simple fix is to use hash(frozenset(self.items())) instead. > > Thanks for pointing out the hash bug. It was an oversight: I meant to > write > > def __hash__(self): > re

Re: frozendict (v0.1)

2010-10-07 Thread kj
In <87hbgxlk67@gmail.com> Arnaud Delobelle writes: >A simple fix is to use hash(frozenset(self.items())) instead. Thanks for pointing out the hash bug. It was an oversight: I meant to write def __hash__(self): return hash(sorted(tuple(self.items( I imagine that frozenset i

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
Oops I sent off my reply before I had finished! kj writes: > Following a suggestion from MRAB, I attempted to implement a > frozendict class. My implementation took a lot more work than > something this simple should take, and it still sucks. So I'm > hoping someone can show me a better way.

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
kj writes: > Following a suggestion from MRAB, I attempted to implement a > frozendict class. My implementation took a lot more work than > something this simple should take, and it still sucks. So I'm > hoping someone can show me a better way. Specifically, I'm hoping > that there is a "recip

frozendict (v0.1)

2010-10-07 Thread kj
Following a suggestion from MRAB, I attempted to implement a frozendict class. My implementation took a lot more work than something this simple should take, and it still sucks. So I'm hoping someone can show me a better way. Specifically, I'm hoping tha