if the else short form

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 2 00:49:39 EDT 2010


On Fri, 01 Oct 2010 11:23:25 -0700, John Nagle wrote:

>> Why so ? The doc clearly states that booleans are integers with True ==
>> 1 and False == 0, so there's nothing implicit here.
> 
>      Python "bool" values are NOT integers.  They can be coerced to
> integers for historical reasons.  

Incorrect. bools *are* ints in Python, beyond any doubt.

>>> isinstance(True, int)
True

True and False are instances of int. That's all you need to know.


> But "str(True)" is "True".

I assume that you're not comparing the literal strings "str(True)" and 
"True" (which would make your claim incorrect). Nevertheless, leaving out 
the quotes is also incorrect:

>>> str(True) is True
False

The only way to get your claim to work is to mix'n'match quotation marks, 
leaving one pair in and dropping the other:

>>> str(True) is "True"
True

But so what? What do you think that proves? All it shows is an 
implementation detail to do with caching of certain small strings:

>>> str(5) is "5"
True



-- 
Steven



More information about the Python-list mailing list