We've looked at the built-in numeric types and the vast number of special methods required to invent a new numeric type. Specialized numeric types that integrate seamlessly with the rest of Python is one of the core strengths of the language. That doesn't make the job easy. It merely makes it elegant and useful when done properly.
When working with numbers, we have a multistep design strategy:
- Consider the built-in versions of complex, float, and int.
- Consider the library extensions, such as decimal and fractions.
For financial calculations, decimal must be used; there is no alternative. - Consider extending one of the preceding classes with additional methods
or attributes. - Finally, consider a novel number. This is particularly challenging since Python's variety of available numbers is already very rich.
Defining new numbers involves several considerations...