Search code examples
pythoncoding-stylevariable-assignment

Assignment with "or" in python


Is it considered bad style to assign values to variables like this?

x = "foobar" or None
y = some_variable or None

In the above example, x gets the value 'foobar'.


Solution

  • No, it's a common practice. It's only considered bad style for expressions that are considerably longer than yours.