Re: __hash__ and ordered vs. unordered collections

2017-11-21 Thread Josh B.
http://bidict.rtfd.io>. Feedback always welcome. Thanks, Josh -- Forwarded message -- From: Raymond Hettinger Date: Mon, Nov 20, 2017 at 4:46 PM Subject: Re: __hash__ and ordered vs. unordered collections To: j...@math.brown.edu If you want to make ordered and unordered coll

Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Chris Angelico
On Tue, Nov 21, 2017 at 6:50 AM, Josh B. wrote: > On Monday, November 20, 2017 at 1:55:26 PM UTC-5, Chris Angelico wrote: >> But what you have is the strangeness of non-transitive equality, which >> is likely to cause problems. > > But this is exactly how Python's built-in dict and OrderedDict beh

Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
On Monday, November 20, 2017 at 2:31:40 PM UTC-5, MRAB wrote: > What if there are duplicate elements? > > Should that be MyColl(some_elements) == MyOrderedColl(other_elements) > iff len(some_elements) == len(other_elements) and set(some_elements) == > set(other_elements)? Yes, that's what I mea

Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
On Monday, November 20, 2017 at 1:55:26 PM UTC-5, Chris Angelico wrote: > But what you have is the strangeness of non-transitive equality, which > is likely to cause problems. But this is exactly how Python's built-in dict and OrderedDict behave: >>> od = OrderedDict([(1, 0), (2, 0), (3, 0)]) >>>

Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread MRAB
On 2017-11-20 17:47, Josh B. wrote: Suppose we're implementing an immutable collection type that comes in unordered and ordered flavors. Let's call them MyColl and MyOrderedColl. We implement __eq__ such that MyColl(some_elements) == MyOrderedColl(other_elements) iff set(some_elements) == set(

Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Chris Angelico
On Tue, Nov 21, 2017 at 4:47 AM, Josh B. wrote: > Now for the question: Is this useful? I ask because this leads to the > following behavior: > unordered = MyColl([1, 2, 3]) ordered = MyOrderedColl([3, 2, 1]) s = {ordered, unordered} len(s) > 1 s = {ordered} unordere

__hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
Suppose we're implementing an immutable collection type that comes in unordered and ordered flavors. Let's call them MyColl and MyOrderedColl. We implement __eq__ such that MyColl(some_elements) == MyOrderedColl(other_elements) iff set(some_elements) == set(other_elements). But MyOrderedColl(so