What does |= (ior) do in Python?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop
--
Chapters
00:00 Question
00:24 Accepted answer (Score 193)
07:26 Answer 2 (Score 113)
07:56 Answer 3 (Score 53)
08:09 Answer 4 (Score 48)
08:37 Thank you
--
Full question
https://stackoverflow.com/questions/3929...
Accepted answer links:
[in-place]: https://docs.python.org/3/library/operat...
[sets]: https://docs.python.org/2/library/sets.h...
[union]: https://en.wikipedia.org/wiki/Union_(set...)
[dicts]: https://docs.python.org/3.9/library/stdt...
[update]: https://docs.python.org/3.9/whatsnew/3.9...
[counters]: https://docs.python.org/3/library/collec...
[union (of multisets)]: https://en.wikipedia.org/wiki/Multiset#B...
[numbers]: https://docs.python.org/3/library/number...
[bitwise OR]: https://docs.python.org/3/reference/expr...
[Python 3.9+]: https://docs.python.org/3.9/whatsnew/3.9...
[multiset]: https://en.wikipedia.org/wiki/Multiset
[union of multisets]: https://en.wikipedia.org/wiki/Multiset#B...
[overloading the ]: https://github.com/python/cpython/blob/5...
[OrderedSet recipe (see lines 3 and 10 respectively)]: https://code.activestate.com/recipes/576.../
[thread on Python-ideas]: https://mail.python.org/pipermail/python...
[section B.8 of Dive in Python 3]: http://www.diveintopython3.net/special-m...
[eval.c]: https://github.com/python/cpython/blob/b...
[abstract.c]: https://github.com/python/cpython/blob/b...
[post]: https://stackoverflow.com/questions/7484...
Answer 2 links:
[bitwise-OR operation]: http://en.wikipedia.org/wiki/Bitwise_ope...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop
--
Chapters
00:00 Question
00:24 Accepted answer (Score 193)
07:26 Answer 2 (Score 113)
07:56 Answer 3 (Score 53)
08:09 Answer 4 (Score 48)
08:37 Thank you
--
Full question
https://stackoverflow.com/questions/3929...
Accepted answer links:
[in-place]: https://docs.python.org/3/library/operat...
[sets]: https://docs.python.org/2/library/sets.h...
[union]: https://en.wikipedia.org/wiki/Union_(set...)
[dicts]: https://docs.python.org/3.9/library/stdt...
[update]: https://docs.python.org/3.9/whatsnew/3.9...
[counters]: https://docs.python.org/3/library/collec...
[union (of multisets)]: https://en.wikipedia.org/wiki/Multiset#B...
[numbers]: https://docs.python.org/3/library/number...
[bitwise OR]: https://docs.python.org/3/reference/expr...
[Python 3.9+]: https://docs.python.org/3.9/whatsnew/3.9...
[multiset]: https://en.wikipedia.org/wiki/Multiset
[union of multisets]: https://en.wikipedia.org/wiki/Multiset#B...
[overloading the ]: https://github.com/python/cpython/blob/5...
[OrderedSet recipe (see lines 3 and 10 respectively)]: https://code.activestate.com/recipes/576.../
[thread on Python-ideas]: https://mail.python.org/pipermail/python...
[section B.8 of Dive in Python 3]: http://www.diveintopython3.net/special-m...
[eval.c]: https://github.com/python/cpython/blob/b...
[abstract.c]: https://github.com/python/cpython/blob/b...
[post]: https://stackoverflow.com/questions/7484...
Answer 2 links:
[bitwise-OR operation]: http://en.wikipedia.org/wiki/Bitwise_ope...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ANSWER 1
Score 114
In Python, and many other programming languages, | is the bitwise-OR operation. |= is to | as += is to +, i.e. a combination of operation and asignment.
So var |= value is short for var = var | value.
A common use case is to merge two sets:
>>> a = {1,2}; a |= {3,4}; print(a)
{1, 2, 3, 4}
ANSWER 2
Score 53
When used with sets it performs union operation.
ANSWER 3
Score 49
This is just an OR operation between the current variable and the other one. Being T=True and F=False, see the output graphically:
| r | s | r|=s |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
For example:
>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True
ANSWER 4
Score 10
It performs a binary bitwise OR of the left-hand and right-hand sides of the assignment, then stores the result in the left-hand variable.
http://docs.python.org/reference/expressions.html#binary-bitwise-operations