What does |= (pipe equal) sign do in python? What does |= (pipe equal) sign do in python? python python

What does |= (pipe equal) sign do in python?


It's a compound operator, when you say: x |= y it's equivalent to x = x | y

The | operator means bitwise or and it operates on integers at the bit-by-bit level, here is an example:

a = 3    #                (011)         #                 |||b = 4    #                (100)         #                 |||a |= b   #<-- a is now 7  (111)

Another example:

a = 2    #                (10)         #                 ||b = 2    #                (10)         #                 ||a |= b   #<-- a is now 2  (10)

So each bit in the result will be set if that same bit is set in either of the two sources and zero if both of the two sources has a zero in that bit.

The pipe is also used on sets to get the union:

a = {1,2,3}b = {2,3,4}c = {4,5,6}print(a | b | c)  # <--- {1, 2, 3, 4, 5, 6}


As @AChampion already mentioned in the first question comment, it could be "bitwise or" or "set union". While this question has Odoo as context, it is "set union" for the Odoo class RecordSet.

This class was introduced with the new API on Odoo 8. For other operators look into the official doc of Odoo.