Assignment with "or" in python

TheOne picture TheOne · Jan 5, 2012 · Viewed 24.2k times · Source

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'.

Answer

tback picture tback · Jan 5, 2012

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