ACCEPTED: PEP 285
Erik Max Francis
max at alcyone.com
Wed Apr 3 21:06:50 EST 2002
djw wrote:
> Pardon my ignorance (I've only been working with Python for a short
> while), but I don't understand why an idiom like:
>
> if x == True:
>
> would be/should be an error (or warning initially).
Because in Python, the canonical way to test for truth, regardless of
what x might be, is:
if x:
...
Even with PEP 285, there are more values which qualify as true than just
True. Explicit testing against one of these true values excludes all
others.
> I see C/C++ code
> like this all the time:
>
> bool x;
> x = Foo(Bar);
> if (x==true)
> {...
This is C++, not C. In C++ also, the proper way to write this is:
if (x)
...
Since the if conditional does an implicit conversion to bool, the
explicit test is unnecessary. The following is completely legal code,
and evaluates the truth clause:
int x = 1;
if (x)
...
In C89, which lacks an explicit Boolean type (although one was added to
C99), the above is also the proper way to test for truth. Just as in
Python, in C++ (and C99) there are more values considered true than just
`true'. In C89 there isn't even an explicit Boolean type.
> Of course, this could also be written in the more abbreviated form as
> well. But, why force that on the programmer? I really prefer the more
> explicit comparison of x with true rather than the if(x) idiom.
Because you're leaving out a whole swatch of values that evaluate to
true in a conditional, they just don't have the explicit value true (of
type bool).
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ Nationalism is an infantile sickness.
\__/ Albert Einstein
Alcyone Systems' Daily Planet / http://www.alcyone.com/planet.html
A new, virtual planet, every day.
More information about the Python-list
mailing list