24

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?

Paolo
  • 20,112
  • 21
  • 72
  • 113
Tanzil Khan
  • 942
  • 1
  • 9
  • 20
  • 5
    It does whatever the type of `moves` decides it does. It generally means "bitwise or" or "set union" and assign and should generally be equivalent to `moves = moves | move` – AChampion Oct 26 '16 at 05:11
  • Looks like the [set union operator](https://docs.python.org/2/library/sets.html). Presumably the `Move` class overloads this to allow for operands of type `Move` – Rob Gwynn-Jones Oct 26 '16 at 05:14
  • FYI: `set` is now a builtin in type: https://docs.python.org/2/library/stdtypes.html#set – AChampion Oct 26 '16 at 05:17
  • I wasn't sure how does the class interact with this operator. Thanks Rob. – Tanzil Khan Oct 26 '16 at 05:34
  • @AChampion thanks for the information. I am trying to follow those operators. :) – Tanzil Khan Oct 26 '16 at 06:27

3 Answers3

14

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}
Bilal
  • 2,883
  • 5
  • 37
  • 60
8

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.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
0

It simply means moves = move | moves.

gmds
  • 19,325
  • 4
  • 32
  • 58