Question For Language Lawyers - WAS: Re: Combining colon statements
peter at engcorp.com
Wed Jul 21 04:55:04 CEST 2004
Christopher T King wrote:
> I remember reading something about this in a previous thread, and I think
> it was decided that int(True) will always evaluate to 1, and int(False)
> will similarly always evaluate to 0. So it may not be good style, but it
> should be guaranteed behaviour for some time. (Note: AFAICT, this isn't
> in the official language spec, though.)
It's in the PEP though (from memory), along with examples of cases
which are to be considered completely legal in Guido's own words...
if the accepted PEPs are not to be considered part of the official
spec (at least until replaced by other documentation), I don't
know what is...
> Not necessarily -- you aren't really assuming they're 1 and 0 in Python
> ((1 is True) == False), but rather that they evaluate to 1 and 0 in an
> integer context, which is something that can be (and it seems is) as well
> defined as the fact that float(1) evaluates to 1.0.
Uh, that "1 is 1" returns true is probably an implementation detail
right now... it's not likely guaranteed. "a = 100" followed by
"a is 100" returns False, after all. (Strange quirk, probably
due to some obscure corner of the compiler, but "a = 100; a is 100"
returns True, as does "100 is 100".)
As usual, identity should be used to compare things unless
you really want identity. In the case of booleans, 1 == True
and always will. Read the PEP again for more detail.
More information about the Python-list