I think you're reading to much into the "is". Sure, "x is True" reads like "x has the property of being true", but that's not what the "is" operator is all about. It's about an identity check that works fine when you want to check for identity, but doesn't otherwise.
For "None" and other singletons, the "is" operator is the perfect choice, since it avoids programming errors and is a very fast way to check a value against the singleton.
For non-singletons, "is" rarely makes sense and can indeed introduce programming errors.
Now, True and False are singletons in their own right, but they are also special integers 1 and 0, nothing more, nothing less.
So if you are interested in checking whether a function does indeed use these special integers, you can use the "is" operator, but apart from testing, where this may sometimes be needed, the much more common use case is not to check for the special integers, but instead check for property of the return value, i.e. whether the return value has the true property or not and you usually apply that check in a different way:
if x: print 'true assumption' else: print 'false assumption'
By checking just for the True singleton, it is well possible that you'll miss a lot of other true values, e.g. 42 is true, but a different integer, so "42 is True" will return false. Likewise, checking for False won't catch the 0.0 float value, an empty string or an empty list.
INADA Naoki wrote:
In : True + True Out: 2
I think Python 4 should raise ValueError.
On Thu, Aug 9, 2012 at 4:35 PM, Yuval Greenfield email@example.com wrote:
On Thu, Aug 9, 2012 at 10:15 AM, Antoine Pitrou firstname.lastname@example.org wrote:
Le 08/08/2012 16:28, Guido van Rossum a écrit :
I'd be strongly against changing that rule. If you don't want other types than bool, use an isinstance check. Code using "is True" is most likely a bug. (It's different for None, since that has no second value that is assumed.)
That said, I'm also curious about the answer to Michael's following question: “why does it say that using an identity check is worse than an equality check?”
In python 3.2.3:
>>> 1 == True True >>> 13 == True False >>> bool(1) True >>> bool(13) True >>> 1 is True False >>> 13 is True False
To my surprise identity is actually less confusing than equality. So I agree with Antoine and Michael on that point.
Python-ideas mailing list Pythonemail@example.com http://mail.python.org/mailman/listinfo/python-ideas