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