Search code examples
pythondocstring

Reasoning behind "x = x or some_constant" in google style


In google's pydoc styleguide I stumbled upon 2.14.4

Yes:

 ...
 def f(x=None):
     if x is None:
         x = []

No:

 ...
 def f(x=None):
     x = x or []

I wonder, why if statement is preferred over x = x or []? Is there some deeper meaning behind this?


Solution

  • If x is false (the boolean value), then it will be replaced with []. Usually you only want to replace when it is actually None.