[Python-Dev] Unhashable objects and __contains__()

Guido van Rossum guido at python.org
Sun Jun 4 00:57:18 CEST 2006


On 6/3/06, Collin Winter <collinw at gmail.com> wrote:
> My question is this: maybe set/frozenset.__contains__ (as well as
> dict.__contains__, etc) should catch such TypeErrors and convert them
> to a return value of False? It makes sense that "{} in frozenset([(1,
> 2, 3])" should be False, since unhashable objects (like {}) clearly
> can't be part of the set/dict/whatever.

Sounds like a bad idea. You already pointed out that it's tricky to
catch exceptions and turn them into values without the risk of masking
bugs that would cause those same exceptions.

In addition, IMO it's a good idea to point out that "{} in {}" is a
type error by raising an exception. It's just like "1 in 'abc'" -- the
'in' operation has an implementation that doesn't support all types,
and if you try a type that's not supported, you expect a type error. I
expect that this is more likely to help catch bugs than it is an
obstacle. (I do understand your use case -- I just don't believe it's
as important as the bug-catching property you'd be throwing away by
supporting that use case.)

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list