Python style: if statements vs. boolean evaluation

Posted by mkscrg on Stack Overflow See other posts from Stack Overflow or by mkscrg
Published on 2010-06-10T05:05:31Z Indexed on 2010/06/10 5:12 UTC
Read the original article Hit count: 151

Filed under:
|
|

One of the ideas of Python's design philosophy is "There should be one ... obvious way to do it." (PEP 20), but that can't always be true. I'm specifically referring to (simple) if statements versus boolean evaluation. Consider the following:

if words:
    self.words = words
else:
    self.words = {}

versus

self.words = words or {}

With such a simple situation, which is preferable, stylistically speaking? With more complicated situations one would choose the if statement for readability, right?

© Stack Overflow or respective owner

Related posts about python

Related posts about style