other = s or "some default value"
Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.
Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.
In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:
42 or "something" # returns 42
0 or "something" # returns "something"
None or "something" # returns "something"
False or "something" # returns "something"
"" or "something" # returns "something"
If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().
In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).
In some languages this behavior is referred to as the Elvis operator.
Answer from Juliano on Stack Overflowother = s or "some default value"
Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.
Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.
In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:
42 or "something" # returns 42
0 or "something" # returns "something"
None or "something" # returns "something"
False or "something" # returns "something"
"" or "something" # returns "something"
If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().
In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).
In some languages this behavior is referred to as the Elvis operator.
Strictly,
other = s if s is not None else "default value"
Otherwise, s = False will become "default value", which may not be what was intended.
If you want to make this shorter, try:
def notNone(s,d):
if s is None:
return d
else:
return s
other = notNone(s, "default value")
Note, though, that since that's a function, it won't short-circuit like the conditional operator would if the first value isn't None; instead, it will evaluate both arguments even through the second one doesn't end up being used. For example, if a is not None, notNone(a, get_some_value()) will still call get_some_value, but a if a is not None else get_some_value() won't (operators can short-circuit, and the conditional operator does).
Python's or operator can be used in a similar way to C#'s ?? operator, but it's even more powerful. It automatically coalesces all falsey values into the first truthy value.
This is pretty useful for setting default values or strings in templates or getters
> x = [] or 3
> x
3
> y = [] or None or 5
> y
5
> z = {} or False or True or 5
> z
TrueVideos
other = s or "some default value"
Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.
Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.
In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:
42 or "something" # returns 42
0 or "something" # returns "something"
None or "something" # returns "something"
False or "something" # returns "something"
"" or "something" # returns "something"
If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().
In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).
In some languages this behavior is referred to as the Elvis operator.
Answer from Juliano on Stack Overflow