[Python-Dev] Suirprise!

Tim Peters tim.one@home.com
Sun, 22 Apr 2001 19:19:19 -0400


[Tim, 'a' in 'a' == 1, etc]

[Guido]
> Yeah, I ran into the same when converting some has_key() tests to
> using 'in'.

Bingo!  Same here, but after adding __iter__ and __contains__ to UserDict.py,
then fiddling test_userdict.py to match.

> I guess it's not very common since nobody in their right minds should
> want to compare the result of an 'in' test to anything else.  The
> has_key() tests did something like "assert d.has_key(k)==1"
> and the mindless translation of that is "assert k in d == 1"...

You'd think so <wink>.  It was subtler in the first I bumped into,
translating something like

    assert d1.has_key(k) == d2.has_key(k)

The problem in

    assert k in d1 == k in d2

is, I think, harder to spot.  That is, you may well be in your right might if
you want to compare the result of an 'in' test to the result of *another*
'in' test!

Even stranger,

    assert k in d1 != k in d2

succeeds if and only if k is in both d1 and d2 (assuming d1 is a dict and k
isn't).  I'm going to use that a lot in my code, because it's one less
character than typing

    assert k in d1 and k in d2

Heh heh.

*something*-about-this-may-not-be-ideal-ly y'rs  - tim