The Python Oracle

PEP 8: comparison to True should be 'if cond is True:' or 'if cond:'

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon

--

Chapters
00:00 Pep 8: Comparison To True Should Be 'If Cond Is True:' Or 'If Cond:'
00:44 Accepted Answer Score 5
01:02 Answer 2 Score 0
01:27 Thank you

--

Full question
https://stackoverflow.com/questions/5081...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pycharm

#avk47



ACCEPTED ANSWER

Score 5


Don't compare boolean against boolean.

You should check if is True or false.

b == true
if b: # If b is True
   do something 

In your case

temp = b > 1.1*a
pos = where(temp)  

Here some explanations




ANSWER 2

Score 0


As per the PEP8 Guidelines in Python, comparing things to True is not a preferred pattern.

temp = True
pcos = where(temp)

If the 'temp' is assigned to be false, still specifying only 'temp' inside a conditional statement will result True. For instance:

temp = False
pros = while(temp) # if or while condition

NOTE: If your code doesn't follow PEP8, this case will not give any error.