Special Method Protocols
__round__() — numeric protocol
Support round(), with optional precision. The hook controls rounding; the sample delegates to float.
Formal Syntax & Structure
python syntaxpython
def __round__(self, ndigits=None):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| ndigits | int | None | Decimal precision, or None for default rounding. |
Return Value
object
Rounded or integral result specified by the hook.
Working Example
Example Snippetpython
import mathclass Number: def __init__(self, value): self.value = value def __round__(self, ndigits=None): return round(self.value, ndigits)print(round(Number(3.125), 2))