Summary
We've looked at the built-in numeric types. We've also looked at 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.
Design considerations and trade-offs
When working with numbers, we have a multistep design strategy:
Consider the built-in versions of
complex
,float
, andint
.Consider the library extensions such as
decimal
andfractions
. For financial calculations,decimal
must be used; there is no alternative.Consider extending one of the above classes with additional methods or attributes.
Finally, consider a novel number. This is particularly challenging, since Python's variety of available numbers is very rich.
Defining new numbers involves several considerations:
Completeness and consistency: The new number must perform a complete set of operations and...