len(var) is [CONSTANT] equal to len(var) == [CONSTANT]?
Tim Roberts
timr at probo.com
Thu Nov 23 16:57:12 EST 2006
Tor Erik Soenvisen <toreriks at hotmail.com> wrote:
>
>(len(['']) is 1) == (len(['']) == 1) => True
>
>Is this the case for all numbers? I've tried running the following:
>
>for i in range(10000):
> for j in range(10000):
> if i != j:
> assert id(i) != id(j), 'i=%d, j=%d, id=%d' % (i, j, id
>(i))
>
>which executes fine. Hence, 0-9999 is okey... But this is a relatively
>small range, and sooner or later you probably get two numbers with the same
>id... Thoughts anyone?
It has been my experience that virtually every use of the "is" operator
(except "is None") is wrong.
Now, I fully understand that there are perfectly valid uses for "is", and
the standard library contains a few, but for the non-guru casual Python
programmer, I think it is safe to say "never use 'is'".
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list