3

Python has inplace operators such as -= and |= for arithmetic and bit operations:

FLAG_FOO = 1 << 0
FLAG_BAR = 1 << 1
mask = FLAG_FOO
mask |= FLAG_BAR
assert mask == 3 == FLAG_FOO | FLAG_BAR

Is there an equivalent for actual True/False Booleans?

z0r
  • 8,185
  • 4
  • 64
  • 83

1 Answers1

4

As mentioned in this question, bitwise & (and) and | (or) work fine for bool variables:

foo = False
foo |= True
assert foo == True == False | True == False or True

When not using inplace operators, it's more idiomatic to use the logical and and or operators. It can be confusing to use the bit operators on Booleans, because e.g. ~True is -2, not False.

Community
  • 1
  • 1
z0r
  • 8,185
  • 4
  • 64
  • 83