Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not True == True:

Tags:

python

The last point of the style guide http://www.python.org/dev/peps/pep-0008

reads ...

Don't compare boolean values to True or False using ==.

Why?

Edit Just to make it clear what im asking (and it's indicative of the problem itself), when you write

if something:
    print "something is true"

You are doing an implicit conversion to a boolean which may or may not work depending on what true means. IMHO this form of programming is discouraged because of the side effects it can cause.

numberOfApples = -1
if numberOfApples:
    print "you have apples" # is not what is intended.

if numberOfApples == True:
    print "you have apples" # is also not what is intended.

iHaveApples = numberOfApples > 0
if iHaveApples is True:    # Edit:  corrected this.. the "is" is better than the ==
  print  "you have apples" # is correct.

Implicit conversions disguise logical errors. So why does the style guide encourage this?

like image 697
Peter Moore Avatar asked Apr 19 '12 06:04

Peter Moore


3 Answers

It means that you should write

if greeting:

Instead of:

if greeting == True:

Similarly, you shouldn't write this either:

if (greeting == True) == True:

The extra tests are redundant and don't add any value to the code, so they should be removed.

like image 158
Mark Byers Avatar answered Oct 03 '22 13:10

Mark Byers


IMHO the point of a style guide is to standardise some common constructs, in a way that makes sense, so you won't end up with wildly disparating statements that ultimately do the same thing. More, the unusual forms might suggest that the programmer had some reason to do things a different way, maybe he was trying to achieve something different than what the statement looks like.

If you want to test the truehood/falsehood of a statement, just use the statement itself or precede it by not. If you must ensure the statement evaluates to True or False (not just a truthy/falsy value), you could use statement is True - though the style guide discourages it - or maybe check its type (using isinstance). But that's usually bad design, you should avoid that unless you have a pretty good reason to do so.

Using statement == True is dangerous for many reasons: 1) only work with True, failing with other "truthy" values (like [1]); 2) might produce unexpected results if the value returned by statement redefines __eq__; 3) might produce different results if the order of the arguments is changed, etc. Note that just using the statement may also return a different value for truehood/falsehood if the value implements __nonzero__ or __len__, but for regular usage that's usually not a problem.

Some examples showing how messed up things can be if you deviate from the style:

if True: print True # True
if 1: print True    # True
if [1]: print True  # True

True is True # True
1 is True    # False
[1] is True  # False

True == True # True
1 == True    # True
[1] == True  # False

Edit: some more:

if 1: print True # True
if 2: print True # True

1 == True # True
2 == True # False

1 is True # False
2 is True # False

Update: as @Marcin pointed out, you can use bool to coerce a value to True/False, guaranteeing that only those values will be present. The result of that function is consistent to the default truehood/falsehood of the value (so __nonzero__ and __len__ is taken into account). Some examples:

if bool(1): print True # True
bool(1) == True        # True
bool(1) is True        # True

if bool(2): print True # True
bool(2) == True        # True
bool(2) is True        # True

1 or 2              # 1
1 and 2             # 2
bool(1) or bool(2)  # True
bool(1) and bool(2) # True

bool(1) == bool(2)  # True
bool(1) is bool(2)  # True
like image 33
mgibsonbr Avatar answered Oct 03 '22 13:10

mgibsonbr


Because it's redundant.

if hasChildren:

is the same as

if hasChildren == True:

but is briefer and easier to read.

like image 26
Andrew Cooper Avatar answered Oct 03 '22 12:10

Andrew Cooper