__truediv__ / __floordiv__
__truediv__(self, other) Any · Updated March 27, 2026 · Dunder Methods Overview
__truediv__ and __floordiv__ let your custom types respond to Python’s division operators. __truediv__ handles / (true division) and always returns a float (or the implementing type’s choice). __floordiv__ handles // (floor division) and conventionally returns an int.
Both methods take self and other as arguments. When you write a / b, Python calls a.__truediv__(b). If that returns NotImplemented, Python then tries b.__rtruediv__(a).
Signature
def __truediv__(self, other) -> Any: ...
def __floordiv__(self, other) -> Any: ...
Return NotImplemented when other is a type you don’t know how to handle. This lets Python try the reversed variant on other before raising a TypeError.
True Division with __truediv__
True division always aims to return a precise fractional result. For Python 3’s built-in types, 10 / 3 gives 3.333... as a float.
class Fraction:
def __init__(self, num, denom):
self.num = num
self.denom = denom
def __truediv__(self, other):
if isinstance(other, (int, float)):
return Fraction(self.num, self.denom * other)
if isinstance(other, Fraction):
return Fraction(self.num * other.denom, self.denom * other.num)
return NotImplemented
def __repr__(self):
return f"Fraction({self.num}, {self.denom})"
f = Fraction(3, 4)
print(f / 2) # Fraction(3, 8)
print(f / 2.0) # Fraction(3, 8.0)
# output: Fraction(3, 8)
# output: Fraction(3, 8.0)
Note that int.__truediv__ always returns a float, even for divisible cases like 10 / 5 which yields 2.0.
Floor Division with __floordiv__
Floor division discards the fractional part and returns an integer (or integer-like type). For 10 // 3, the result is 3.
class Vector:
def __init__(self, x, y):
self.x, self.y = x, y
def __floordiv__(self, scalar):
if isinstance(scalar, (int, float)):
return Vector(self.x // scalar, self.y // scalar)
return NotImplemented
def __repr__(self):
return f"Vector({self.x}, {self.y})"
v = Vector(10, 7)
print(v // 3) # Vector(3, 2)
# output: Vector(3, 2)
Reversed Variants: __rtruediv__ and __rfloordiv__
When Python evaluates a / b and a.__truediv__(b) returns NotImplemented, Python calls b.__rtruediv__(a). The same pattern applies to floor division.
These reversed variants let you handle cases where the left operand is a built-in type and the right operand is your custom type.
class MyNum:
def __init__(self, val):
self.val = val
def __truediv__(self, other):
if isinstance(other, (int, float)):
return MyNum(self.val / other)
return NotImplemented
def __rtruediv__(self, other):
if isinstance(other, (int, float)):
return MyNum(other / self.val)
return NotImplemented
def __repr__(self):
return f"MyNum({self.val})"
m = MyNum(2)
print(10 / m) # Calls m.__rtruediv__(10) → MyNum(5.0)
print(m / 2) # Calls m.__truediv__(2) → MyNum(1.0)
# output: MyNum(5.0)
# output: MyNum(1.0)
Full Example with Both Methods
Combining __truediv__ and __floordiv__ in one class gives you full control over both division operators.
class Vector:
def __init__(self, x, y):
self.x, self.y = x, y
def __repr__(self):
return f"Vector({self.x}, {self.y})"
def __truediv__(self, scalar):
if isinstance(scalar, (int, float)):
return Vector(self.x / scalar, self.y / scalar)
return NotImplemented
def __floordiv__(self, scalar):
if isinstance(scalar, (int, float)):
return Vector(self.x // scalar, self.y // scalar)
return NotImplemented
def __rtruediv__(self, scalar):
if isinstance(scalar, (int, float)):
return Vector(scalar / self.x, scalar / self.y)
return NotImplemented
def __rfloordiv__(self, scalar):
if isinstance(scalar, (int, float)):
return Vector(scalar // self.x, scalar // self.y)
return NotImplemented
v = Vector(10, 6)
print(v / 2) # Vector(5.0, 3.0)
print(v // 2) # Vector(5, 3)
print(30 / v) # Vector(3.0, 5.0)
print(30 // v) # Vector(3, 5)
# output: Vector(5.0, 3.0)
# output: Vector(5, 3)
# output: Vector(3.0, 5.0)
# output: Vector(3, 5)
Mixed Type Behavior
If you define only __truediv__ but not __floordiv__, the // operator will raise TypeError when called on your type, unless the right operand handles the reversed case. Similarly, defining only __floordiv__ limits your type’s use with /.
Python’s built-in types behave predictably: int.__truediv__ always returns float, int.__floordiv__ returns int, and float.__floordiv__ returns float.