tags:

views:

208

answers:

5

In python:

>>> a = b or {}
+1  A: 

That's not a special construct, at least to my somewhat tired eyes. It's saying to assign the result of

 (b or {}) 

to a.

So, if b is a False value, a gets {} (the empty dictionary literal), otherwise it gets the value of b.

Steven Schlansker
exactly, but maybe there's a name for it. Short-circuit assignment is the one I would be tempted to say. maybe conditional assignment ? But that normally refers to the ?: operator.
Stefano Borini
It's not really relying on any sort of short-circuiting, though... I'd go for that if it was suppressing some side effect on the RHS. But for this?
Steven Schlansker
It's a "special construct" if you're coming from C++ or Java where the only things you can put on either side of an "or" are booleans. Here, b could be any object (presumably a dictionary based on the context) so (b or {}) is *not* a boolean. Perfectly normal for a Pythonista, but not obvious if you're learning Python for the first time.
MatrixFrog
+9  A: 

I don't think it has an official name, it's just a clever/lazy way to be concise. It's roughly equivalent to:

a = b if b else {}

or:

if b:
    a = b
else:
    a = {}

I wrote this as a comment but I think it's worth mentioning here:

You have to be very careful when using this trick. If your intention is to set a default value when something is None, it would be better to write that is None test explicitly than use this trick. The reason is that None is not the only thing that evaluates to false in a boolean context. 0, [], {}, ... also evaluate to false. So this trick might not give you the expected results if you didn't think about these special cases.

Here's one way to do it that is safer:

a = b if b is not None else {}
Mark Byers
so let's give him one :)
Stefano Borini
Pretty much like var a = b ?? new b() in C#.
Filip Ekberg
@Filip: Similar, except the null-coalescing operator `??` was added to C# intentionally for that purpose. This trick in Python works because None is treated as false in a boolean context. But 0, [], {}, etc... also evaluate to false, so be careful. If you want to emulate `??`, it's better to write the `is None` test explicitly than rely on this trick.
Mark Byers
It was often combined with related "and" behaviour to get the effect of "a = b if c else d" in the past. The pattern often led to bugs, which is part of the reason for adding the conditional expression. I don't remember what the common edge cases were - that's part of the issue, they aren't obvious.
Steve314
@Steve314: The danger with `r = a and b or c` is when a is True but b is 0 the expression evaluates to c instead of 0 as you might expect.
Mark Byers
+1  A: 

May be The expression that return last evaluated argument.

S.Mark
+1  A: 

Default Assignment Idiom

gnibbler
official or invented by you at this very moment ?
Stefano Borini
@Stefano, I thought we are voting for what we think the official name should be :)
gnibbler
+2  A: 

The expression b or {} is just a simple boolean OR expression. The special about this is just that this expression does not need return a boolean value per definition like in other languages:

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., not 'foo' yields False, not ''.)

Gumbo