0

I just came across an assignment like this in python. I checked everywhere to see what this was but couldn't find it. What does this mean in python?

 total_mask |= is_NA

The operator in the middle is confusing me.

Thanks

4
  • wiki.python.org/moin/BitwiseOperators Commented Jul 25, 2014 at 18:21
  • 3
    Generally speaking, A ?= B is the same as A = A ? B, for most any operator you care to replace the question mark with. Commented Jul 25, 2014 at 18:21
  • That operator (and many like it) were introduced in PEP 203. Commented Jul 25, 2014 at 18:22
  • docs.python.org/3/reference/… Commented Jul 25, 2014 at 18:26

2 Answers 2

3

It's just an operator of logical OR. If total_mask is

0011101010101001110

and is_NA is

0000000000000100000

then total_mask will become

0011101010101101110

in this case it's just like addition. EDIT: of course the numbers are here written in binary. In Python you can convert a number to a binary string using the bin function, for example

>>> bin(99)
'0b1100011'
Sign up to request clarification or add additional context in comments.

Comments

2

It is the same as

total_mask = total_mask|is_NA

2 Comments

@wim care to explain?
consider the case of sets, where this operator means in-place union. the |= operator (which slots into set.__ior__) will preserve the identity of the set, whereas the method you have shown will create a new set. this can have side effects for other references to the same set.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.