Dual look-up on keys?

castironpi at gmail.com castironpi at gmail.com
Wed Mar 5 23:23:45 EST 2008


On Mar 5, 8:03 pm, castiro... at gmail.com wrote:
> On Mar 5, 5:31 pm, Grant Edwards <gra... at visi.com> wrote:
>
> > On 2008-03-05, castiro... at gmail.com <castiro... at gmail.com> wrote:
> > > Anyway, if (a,b) is a key in dictionary d, can it guarantee
> > > that (b,a) is also in it, and maps to the same object?
>
> Er... -specialized- dictionary d.
>
> > To solve that problem, Python provides the immutable
> > "frozenset" type:
>
> >   >>> s1 = frozenset((1,2))
> >   >>> s2 = frozenset((2,1))
> >   >>> s1 == s2
> >   True
> >   >>> d = {s1: "hi there"}
> >   >>> s1 in d
> >   True
> >   >>> s2 in d
> >   True
>
> Ah.  Perfect.  With this, I can just call frozenset on keys in
> __setitem__ and __getitem__... (though at that, it may be easier
> verbatim*.)

> a= SomeList( [ 1,2,3 ] )
> b= SomeList( [ 1,2,3 ] )
> assert a is not b
> d[a]= True
> assert b not in d
> #there's the hangup

*plonk*

key is an iterable, just like the constructors to <bleep bl bbl bleep>
other collection.

>>> 2 in d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

Interface first?  In YOUR face first!



More information about the Python-list mailing list