Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical bar in Python bitwise assignment operator

Tags:

There is a code and in class' method there is a line:

object.attribute |= variable 

I can't understand what it means. I didn't find (|=) in the list of basic Python operators.

like image 936
Olga Avatar asked Jan 20 '14 20:01

Olga


People also ask

What does a vertical bar mean in Python?

Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two >>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101' Pythonista. © Copyright 2022.

How do you make a vertical bar in Python?

It is located right above the Enter key. Another way to type the vertical bar character is to turn on the numeric keypad, hold ALT , then press 1, 2, and 4.

What does a bar do in Python?

A bar plot shows catergorical data as rectangular bars with the height of bars proportional to the value they represent. It is often used to compare between values of different categories in the data.

What does a straight line mean in Python?

(|) is a Bitwise OR operator if any one or both of the bits is 1, it results 1 if both the bits are zero 0, it results in 0 10|20 = 30 Binary Decimal 0000 1010 10 | 0001 0100 20 -------------------------------------- 0001 1110 30 1|0 = 1 1&0 = 0 1|1 = 1 1&1 = 1 0|0 = 0 0&0 = 0 Check out the lesson 👇 https://www. ...


1 Answers

That is a bitwise or with assignment. It is equivalent to

object.attribute = object.attribute | variable 

Read more here.

like image 156
Elliott Frisch Avatar answered Sep 18 '22 04:09

Elliott Frisch