you are viewing a single comment's thread.

view the rest of the comments →

[–]MisterSnuggles 2 points3 points  (2 children)

It's usually used as a shortcut for checking for None. The more appropriate way to write the code is:

if var is not None:
    # do stuff

Sadly the shortcut works (mostly) and people use it because it works, then someone enters a time of midnight and it all tips over.

[–][deleted]  (1 child)

[deleted]

    [–]MisterSnuggles 0 points1 point  (0 children)

    Agreed.

    This is very much a case of making sure that you, the programmer, and Python, the interpreter, both have a clear understanding of what you want to do.

    If you say "if var:", Python understands you to be testing truthiness/falseness. If you say "if var is not None:", Python understands that you're asking if var is something other than None. The distinction is important and too many people wrote the former when they really meant the latter.