Avoiding Unnecessary Negative Logic
You need to create the file if it doesn’t exist, but if this condition is true, there’s nothing that needs to be done. So you use
pass to indicate an empty block, but this isn’t very Pythonic code.
But that doesn’t mean negative logic is always good. Let’s look at a custom absolute value program that some programmer may have written. The programmer wants to focus on the less than
Here’s another example of unnecessary negative logic with an inequality check. The programmer uses
not along with the
== (is equal to) operator, but Python has an
!= (is not equal to) operator, and it would be much better to use that in this situation.
Become a Member to join the conversation.