Python Glossary Русский

all Python 3operatorfunction

operator.or_

bitwise

Bitwise OR: a | b (the underscore avoids clashing with `or`).

Syntax

operator.or_(a, b)

This is bitwise |, not logical or: there is no short-circuit, or_(1, 2) is 3 while 1 or 2 is 1. For sets or_ is union; for dicts since Python 3.9 it merges, and the right side wins on a shared key: or_({'a': 1}, {'a': 2}) returns {'a': 2}. To union many sets: functools.reduce(operator.or_, sets, set()).

Examples

import functools
import operator
print(operator.or_(5, 2))   # → 7
print(operator.or_(5, 2) == 5 | 2)   # → True
print(functools.reduce(operator.or_, [1, 2, 4, 8]))   # → 15
print(operator.or_({1, 2}, {2, 3}) == {1, 2, 3})   # → True
print(operator.or_(2, 4) == (2 or 4))   # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy