r/Python • • Jun 23 '14

Writing better python code

https://gist.github.com/kracekumar/09a60ec75a4de19b346e
143 Upvotes

126 comments sorted by

View all comments

12

u/steve_no Jun 23 '14

The normal code "is" example seems strange. I think the non-pythonic thing most programmers do with respect to "is" is not using it, for example:

if a == None:

when they should be doing:

if a is None:

0

u/pymag09 Jun 23 '14 edited Jun 23 '14

I think
if not a:
is better

15

u/pbaehr Jun 23 '14

Depends. Could yield a false negative if a can be 0 or an empty string and that's not what you are interested in testing for.

6

u/bheklilr Jun 23 '14

I also prefer the explicit if a is None, since it signals my intent better. I honestly try to only use if a: when a is a boolean, since I don't want to have to remember how __bool__ is implemented for every type out there. In my opinion, explicit is better than implicit for being able to read your own code in a month's time.