1

Here is a piece of code:

if resp.email is None or resp.email == "":

I think the following is clearer:

if not resp.email:

Does the first option have any advantage to the second one?

My main concern about the second option is the possibility of hiding bugs if resp.email is False or "". Isn't this a very real scenario in production code?

Is there a widely accepted standard or convention on this matter?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • Possible duplicate of [if A vs if A is not None:](http://stackoverflow.com/questions/7816363/if-a-vs-if-a-is-not-none) – Nir Alfasi Oct 05 '15 at 22:45

3 Answers3

3

You should always aim for simplicity (which leads to clarity as well), if something: is simpler than if something is not None: (double negative) so you should use the first. If you run into a specific case where something has a falsy value and you want to check for that value vs. None you can check: if something is False: which is not only clearer, but also more explicit (and hence easier for the reader to understand).

By writing if A is not None: you don't explain to the reader what is the domain of values - which makes it implicit and vague.

Same applies for if list: which should be preferable over if len(list) > 0: which is unnecessarily verbose and complicates the condition.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • What happened to *'explicit is better than implicit'*? – Aviv Cohn Oct 05 '15 at 21:59
  • I think you should always aim for _clarity_, which sometimes translates to "simple", sometimes not. To me, `is not None` is crystal clear; `if something` always makes me wonder if there is a typo there and the writer meant to add a condition. – Bryan Oakley Oct 05 '15 at 22:08
  • @AvivCohn simple usually *is* clearer. I find it difficult to find a counterexample... as a python developer `if list:` should be very clear, much clearer than `if list is not None:` – Nir Alfasi Oct 05 '15 at 22:10
  • @BryanOakley same response applies to you ^ ;) – Nir Alfasi Oct 05 '15 at 22:11
  • `if not` may accidentally catch `False` and empty lists or strings. Couldn't this hide bugs? – Aviv Cohn Oct 05 '15 at 22:14
  • @AvivCohn you're asking, in other words, "does a developer need to do what he/she is doing?" and the answer is definitely **yes**! – Nir Alfasi Oct 05 '15 at 22:43
  • @AvivCohn I added a paragraph that explains why the approach above actually *is* more explicit. – Nir Alfasi Oct 05 '15 at 22:54
3

There is not going to be a universal answer, as the answer is going to depend on what you're trying to do.

For instance, in your first example, let's say something = 0. if something: will evaluate as False, but if something is not None: will evaluate to True. Same for the empty string, empty list, etc.

So are you looking for anything that's not equivalent to the Boolean True? Use the first test. On the other hand, if you only want to exclude None, use the second.

edit: okay, you completely changed your question, but my answer still holds. The answer depends on weather or not 0, False, [], etc are valid values. I suspect not, so I would probably use the if not test.

BlivetWidget
  • 10,543
  • 1
  • 14
  • 23
-2

according to: https://www.python.org/dev/peps/pep-0008/#programming-recommendations, it seems that the is not style is preferred due to readability

EDIT:

I do realize that this is not quite the same thing, as you are comparing to not having a not None, but I feel like it translate the same way (ie, it is still more readable)

RESPONSE TO QUESTION EDIT (empty list):

this case is actually mentioned in the above link, and "you should use the fact that empty sequences are false:

if not list:

instead of:

if not len(list):
R Nar
  • 5,465
  • 1
  • 16
  • 32
  • it says: "Use is not operator rather than not ... is " it doesn't discuss: `is not None` vs. `is` – Nir Alfasi Oct 05 '15 at 21:53
  • check the edit, it still increases readability in the same way as it is more concise in what it is asking for – R Nar Oct 05 '15 at 21:54
  • I disagree, it does not increase readability because it makes the condition unnecessarily more complex. – Nir Alfasi Oct 05 '15 at 21:57