__mod__ — Easy Examples
Defines behavior for the % modulo operator
Implementing __mod__
Basic implementation of __mod__ in a class.
python
class Example: def __mod__(self): return "Example __mod__" obj = Example() print(obj)
__mod__ defines behavior for the % modulo operator. Implementing it lets you customize how Python interacts with your objects.
__mod__ in action
Seeing __mod__ called by Python's built-in operations.
python
# How Python calls __mod__ automatically class Demo: def __init__(self, value): self.value = value def __mod__(self, other): print(f"__mod__ was called!") return self d = Demo(42) # This triggers __mod__: print(d)
Python automatically calls __mod__ when you use the corresponding operator or function on your object.
Want to try these examples interactively?
Open Easy Playground