question about True values

Grant Edwards grante at visi.com
Thu Oct 26 01:46:32 CEST 2006


On 2006-10-25, John Coleman <jcoleman at franciscan.edu> wrote:
>
> Paul Rubin wrote:
>> John Salerno <johnjsal at NOSPAMgmail.com> writes:
>> > I'm a little confused. Why doesn't s evaluate to True in the first
>> > part, but it does in the second? Is the first statement something
>> > different?
>>
>> No.  True and False are boolean values, where booleans are a different
>> data type from strings, just like strings are different from integers.
>>
>>   >>> if s:
>> 	print 'hi'
>>
>> converts s to a boolean during evaluation.  That is, it's the same as
>>
>>     if bool(s): print 'hi'
>>
>> bool(s) is a function that converts s to a bool.  If s is a string,
>> bool(s) is true if s is nonempty, false otherwise.
>>
>> A comparable thing with integers: suppose
>>
>>    x = 3.1
>>
>> then "x == 3" is false, but "int(x) == 3" is true.
>
> But then why is 3.0 == 3 true? They are different types.

3 gets promoted to a float.  In most (all?) current
implementations, that turns out to be 3.0, but that's not
guaranteed.

It could be argued that promotion of integer types to floats
and shorter integers to longer integers is "a bad thing" in
what's supposed to be a strictly typed language. But, it's one
of those things that done that way because it's always been
done that way.

While it does make life simpler in many cases, it's also a
source of bugs in others.

-- 
Grant Edwards                   grante             Yow!  Why is it that when
                                  at               you DIE, you can't take
                               visi.com            your HOME ENTERTAINMENT
                                                   CENTER with you??



More information about the Python-list mailing list