What does the "|=" operator mean in Python?

Asked

Viewed 2,129 times

15

I was analyzing a code and came across the operator |=, would like to know what this means, and what is its practical application.

Example:

x |= y 

2 answers

15


Just like the operator += increments the value of a variable, something like x +=y be equivalent to x = x + y, the operator |= executes the or binary between the current value and the other parameter, something like x |= y be equivalent to x = x | y.

or binary is nothing more than the operation or executed bit by bit. For example, let’s take two positive integers:

x = 5
y = 13

We can verify the binary representation of each value through the native function bin.

print(bin(x), bin(y)) # 0b101 0b1101

Which can be expanded to 8 bits for 00000101 and 00001101. The binary or will do the operation or bit by bit, returning 1 when at least 1 of the operands is 1 or 0 when both are 0.

00000101 | 00001101 = 00001101

Note that the result, in this example, will be equal to the second operand, then do 5 | 13 results in 13. Thus:

x |= y
print(x) # 13

See working on Ideone.

Applying

One of the most common applications of this operator is to define flags used in some system configurations. Flags are commonly boolean values, which can only receive 0 or 1 values. Using the Boolean type, it is common for compilers and interpreters to allot more than 1 bit for such a type, even if it does occupy a single bit. To circumvent this, it is possible to store all flags in another type of variable, using the operator |. whereas the type char is represented by 8 bits, which is the default in C, it would be possible to store 8 bits flags different in one char. With Python, because it is dynamic typing, it ends up being a little abstract, but the philosophy behind it ends up being the same.

Let’s see an example with code:

Let’s consider that our application has 8 different configuration options. To simplify, I will give generic names from A to H:

CONFIG_A = 1     # bin: 0b00000001
CONFIG_B = 2     # bin: 0b00000010
CONFIG_C = 4     # bin: 0b00000100
CONFIG_D = 8     # bin: 0b00001000
CONFIG_E = 16    # bin: 0b00010000
CONFIG_F = 32    # bin: 0b00100000
CONFIG_G = 64    # bin: 0b01000000
CONFIG_H = 128   # bin: 0b10000000

Note that with these certain integer values, each configuration will represent only one bit in the binary representation. This will allow if our configuration has the value 0b00000101, will mean the settings CONFIG_A and CONFIG_C were defined. This definition can be done through the |:

config = CONFIG_A | CONFIG_C  # bin: 0b00000101

See working on Ideone.

In the code, to check if a certain configuration has been set, just use the reverse operation & (bit by bit). For example:

def foo(config):
    if (config & CONFIG_A): print("Configuração A foi definida")
    if (config & CONFIG_B): print("Configuração B foi definida")
    if (config & CONFIG_C): print("Configuração C foi definida")
    if (config & CONFIG_D): print("Configuração D foi definida")
    if (config & CONFIG_E): print("Configuração E foi definida")
    if (config & CONFIG_F): print("Configuração F foi definida")
    if (config & CONFIG_G): print("Configuração G foi definida")
    if (config & CONFIG_H): print("Configuração H foi definida")
    print(" -- + --")

Testing the function:

foo(CONFIG_A)
foo(CONFIG_B | CONFIG_D)
foo(CONFIG_H | CONFIG_E | CONFIG_F)

The exit will be:

Configuração A foi definida
 -- + --
Configuração B foi definida
Configuração D foi definida
 -- + --
Configuração E foi definida
Configuração F foi definida
Configuração H foi definida
 -- + --

See working on Ideone.

  • I suspected from the beginning! The explanation of the application was perfect (+1)

10

For integers, it is the bit-by-bit compound assignment operator or OR. Like all compound assignment operators, a | = b is the same thing as a = a | b. Where the | is the symbol of OR bit by bit.

|= Bitwise including OR https://en.wikipedia.org/wiki/Augmented_assignment

|= (bitwise OR assignment) Performs bitwise OR and assigns value to the left operand. http://python-reference.readthedocs.io/en/latest/docs/operators/

My understanding after researching... The |= will assign the value after executing a OR between two variables, but bit by bit.

For example:

   0101 (decimal 5)
OR 0011 (decimal 3)
 = 0111 (decimal 7)

Another example:

a |= 10 //Supondo que a vale 12.

   1100 (decimal 12)
OR 1010 (decimal 10)
 = 1110 (decimal 14)

//Dessa forma a será igual a 14.

From a glance at this link I took it as a base. It is the same question as yours with enough examples and use case: https://stackoverflow.com/questions/18884075/usecase-of-in-python

  • Good answer (+1), lacking only to exemplify with a practical application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.