What does |= (pipe equal) sign do in python?
Question:
I saw a piece of code in a project where following is written:
move = Move.create({
'name': repair.name,
'product_id': repair.product_id.id,
'product_uom': repair.product_uom.id or repair.product_id.uom_id.id,
'product_uom_qty': repair.product_qty,
'partner_id': repair.address_id.id,
'location_id': repair.location_id.id,
'location_dest_id': repair.location_dest_id.id,
'restrict_lot_id': repair.lot_id.id,
})
moves |= move
moves.action_done()
What does the |=
meaning here?
Answers:
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
.
For your example: It’s the same as moves = moves | move
and means a union of moves
and move
.
This class was introduced with the new API on Odoo 8. For other operators look into the official doc of Odoo.
It simply means moves = move | moves
.
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}
I saw a piece of code in a project where following is written:
move = Move.create({
'name': repair.name,
'product_id': repair.product_id.id,
'product_uom': repair.product_uom.id or repair.product_id.uom_id.id,
'product_uom_qty': repair.product_qty,
'partner_id': repair.address_id.id,
'location_id': repair.location_id.id,
'location_dest_id': repair.location_dest_id.id,
'restrict_lot_id': repair.lot_id.id,
})
moves |= move
moves.action_done()
What does the |=
meaning here?
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
.
For your example: It’s the same as moves = moves | move
and means a union of moves
and move
.
This class was introduced with the new API on Odoo 8. For other operators look into the official doc of Odoo.
It simply means moves = move | moves
.
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}