The __or__ method is the special method behind the | operator. When you write a | b, Python calls a.__or__(b), and your class decides what the result is: a bitwise OR for flag types, a union for set-like types, or a merge for mapping types. Return NotImplemented when you do not support the other operand's type.
The | operator is also used for dictionary merging (Python 3.9+) and for union types in annotations such as int | None (Python 3.10+), which makes __or__ more relevant than it looks.
| operatorBitVector__ror__ and __ior____or__ for union and merge semantics__or__ methods| operatorFor the expression a | b, Python follows this order:
type(a).__or__(a, b).NotImplemented, it calls the reflected method type(b).__ror__(b, a).TypeError: unsupported operand type(s) for |.If type(b) is a subclass of type(a) and overrides __ror__, Python tries the subclass's __ror__ first. There is also an in-place form, __ior__, used by a |= b. If it is missing, Python falls back to a = a | b.
| Expression | Method called first | Fallback |
|---|---|---|
a | b | a.__or__(b) | b.__ror__(a) |
a |= b | a.__ior__(b) | a.__or__(b), then b.__ror__(a) |
BitVectorThe simplest implementation combines two instances of the same class.
The type check matters. Without it, bv1 | 5 would crash with an AttributeError instead of the clear TypeError Python raises when both sides return NotImplemented.
__ror__ and __ior__Often you want flags | 4 and 4 | flags to both work. The first calls __or__. The second needs __ror__, because int.__or__ does not know your class.
Because OR is commutative, __ror__ can reuse __or__. For a non-commutative operation, write it separately and mind the operand order.
__or__ for union and merge semanticsThe operator is not limited to bits. The built-in set uses | for union, and dict uses it for merging. A custom collection can follow the same convention.
For bit flags with names, prefer enum.Flag or enum.IntFlag. They implement |, &, ^ and ~ for you and print readable names.
The exact text printed for a combined flag depends on the Python version (3.11 changed the format), so avoid parsing it.
__or__ methodsself. Mutation belongs in __ior__.NotImplemented, never raise TypeError yourself, when the operand type is unsupported.__ror__ when mixed-type expressions should work in both orders.| for unrelated meanings; a method such as merge() is clearer.__or__ and __ror__?__or__ handles obj | other, where your object is on the left. __ror__ handles other | obj, where your object is on the right and the left operand did not know how to handle it.
__or__ the same as the or keyword?No. The or keyword is boolean short-circuit logic and cannot be overloaded. Only the | operator is customised through __or__; truthiness is controlled by __bool__ and __len__.
dict | dict work?Since Python 3.9, dict defines __or__ to return a new dictionary where the right-hand values win for duplicate keys. |= updates the left dictionary in place.
int | None work in type hints?Since Python 3.10, the type class defines __or__, so int | None builds a types.UnionType. It is equivalent to typing.Optional[int].
__or__ implements a | b. Check the operand type, return NotImplemented when you cannot handle it, and add __ror__ and __ior__ when mixed or in-place use makes sense. For named bit flags use enum.Flag, and for plain unions or merges follow the semantics of set and dict.