Special Method Protocols
__divmod__() — numeric protocol
Implement the left operand hook for quotient and remainder (divmod()). Return NotImplemented for an unsupported operand type to permit reflected dispatch.
Formal Syntax & Structure
python syntaxpython
def __divmod__(self, other):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| other | object | Other operand. |
Return Value
object | NotImplementedType
Operation result, or NotImplemented to decline.
Working Example
Example Snippetpython
class Number: def __init__(self, value): self.value = value def __divmod__(self, other): if not isinstance(other, int): return NotImplemented return divmod(self.value, other)print(divmod(Number(7), 3))