__floordiv__Easy Examples

Defines behavior for the // floor division operator

Implementing __floordiv__

Basic implementation of __floordiv__ in a class.

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

obj = Example()
print(obj)

__floordiv__ defines behavior for the // floor division operator. Implementing it lets you customize how Python interacts with your objects.

__floordiv__ in action

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

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

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

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

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

Want to try these examples interactively?

Open Easy Playground