What makes a sign | = (pipe equal sign) in python?

I saw a piece of code in a project where the 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 it mean here | = value?

+10
source share
3 answers

As @AChampion already mentioned in the first interrogative commentary, it can be “bitwise” or “established pooling”. Although this question has Odoo as a context, it "sets the join" for the Odoo RecordSet class.

This class was introduced with the new Odoo 8 API. For other operators, take a look at the Odoo white paper.

+4
source

This is a compound statement when you say: x |= y is equivalent to x = x | y x = x | y

Operator | means bitwise or , and it works with integers at the 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) 

Thus, each bit will be set as a result if the same bit is set in either of the two sources, and is equal to zero if both sources have zero in this bit.

Pipe is also used on sets to get a mix:

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

It just means that moves = move | moves moves = move | moves moves = move | moves moves = move | moves

-one
source

Source: https://habr.com/ru/post/1011702/


All Articles