__or__Easy Examples

Defines behavior for the | bitwise OR operator

Implementing __or__

Basic implementation of __or__ in a class.

python
class Example:
    def __or__(self):
        return "Example __or__"

obj = Example()
print(obj)

__or__ defines behavior for the | bitwise or operator. Implementing it lets you customize how Python interacts with your objects.

__or__ in action

Seeing __or__ called by Python's built-in operations.

python
# How Python calls __or__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __or__(self, other):
        print(f"__or__ was called!")
        return self

d = Demo(42)
# This triggers __or__:
print(d)

Python automatically calls __or__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground