69

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

9
  • 5
    That's not what a "side effect" is. Commented Jan 5, 2012 at 18:42
  • 4
    @Ramin I know you're not assigning a boolean. My point is that I don't see any side effect. Commented Jan 5, 2012 at 19:28
  • 5
    @Ramin: "A somewhat unusual behavior", "side effect" has a very different meaning in computer science. en.wikipedia.org/wiki/Side_effect_(computer_science) Commented Jan 5, 2012 at 19:47
  • 4
    Yes Python has them, terrible, dark, dangerous (a Haskeller) Commented Jan 5, 2012 at 20:19
  • 2
    Related: What does an 'x = y or z' assignment do in Python? Commented May 4, 2018 at 13:55

4 Answers 4

50

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is considered an anti-pattern in JavaScript. It does not read naturally to uninitiated and you cannot assign falsy values.
@diginoise Nothing in Javascript or any other language reads naturally to the sufficiently uninitiated. But once a developer is familiar with short-cut evaluation (as used in, e.g., if foo or bar..., which is quite a common idiom in JS, Python and many other languages) it reads just as naturally, though you may have to think about it for a moment the very first time you see it. However, "falsy" values are a problem in Python, too, as with 'list = []; return list or False`
35

The primary danger of doing something like this is the possibility that (in the second case) some_variable is False but not None (the integer 0, for instance) and you don't want to end up with y equal to None in that case.

Comments

14

OP's syntax is perfectly fine.
The official name for "assignment with or" is null coalescing and there's actually a Wikipedia page about it now! https://en.wikipedia.org/wiki/Null_coalescing_operator
This question may be useful as well: Is there a Python equivalent of the C# null-coalescing operator?

Comments

11

I also feel a bit unconfortable using that kind of expressions. In Learning Python 4ed it is called a "somewhat unusual behavior". Later Mark Lutz says:

...it turns out to be a fairly common coding paradigm in Python: to select a nonempty object from among a fixed-size set, simply string them together in an or expression. In simpler form, this is also commonly used to designate a default...

In fact, they produce concise one-line expressions that help to eliminate line noise from the code.
This behavior is the basis for a form of the if/else ternary operator:

A = Y if X else Z

2 Comments

But it may cause to repeat a long parameters where Y = X. For instance: A = my_main_dictionary.my_sub_dictionary.my_value if my_main_dictionary.my_sub_dictionary.my_value else None or A = my_main_dictionary.my_sub_dictionary.my_value or None
@RoeeGavirel just assign my_value to a local variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.