Instead of writing
a = a or b
for Booleans a and b, I would like to write something like:
a |= b (or equals)
but this doesn't work. Is there an equivalent to this concise statement in Python? Thanks.
Please note that
a = a or b
Won't return a boolean per se, but will return a if evaluated truthy (so if it's -1, 1, "a", obj, etc)
| In python (and most languages) is a Bitwise operator
I'm afraid a or b is as short as it gets. But it's already much better than using ternary expression like in C
condition ? expr1 : expr2
In Python
a = a if a else b
a |= bdoes work, please show an example where it doesn'toracts like a null coalescing operator whereas|does bitwise or in the second one. Do print3 or 5and3 | 5and compare results.a |= bworks iff both are booleans. For everything else usea = a or b, ora |= bool(b)ifais a boolean.