Operators
|= — Bitwise OR and union assignment
Combine integer bits, update a mutable set by union, or merge entries into a dictionary (Python 3.9+). Dictionary |= accepts a mapping or an iterable of key-value pairs, unlike ordinary dictionary | which requires dictionaries for built-in operands. Later values replace earlier ones without moving existing keys. The __ior__() hook may modify its receiver or supply a replacement. If absent or declining with NotImplemented, normal binary dispatch applies. The result is stored back into the target; this is a statement, not an expression. A failed store does not guarantee rollback.
Formal Syntax & Structure
python syntaxpython
target |= expressionParameters & Attributes
| Name | Type | Description |
|---|---|---|
| target | object | Existing assignable name, attribute, item or slice; its current value participates in the operation. |
| right | integer, compatible set, mapping or pair iterable | Value used as the right operand of the operation. |
Working Example
Example Snippetpython
settings = {"mode": "light", "size": 2}alias = settingssettings |= [("mode", "dark"), ("zoom", 3)]print(alias)print(settings is alias)flags = 0b0100flags |= 0b0011print(flags)