The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 Question
00:52 Accepted answer (Score 5)
01:14 Answer 2 (Score 0)
01:47 Thank you

--

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

Accepted answer links:
[Here]: https://faisalferoz.wordpress.com/2010/0.../

--

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.