Defining number classes

In some cases, we might want to extend the numeric tower available in Python. A subclass of numbers.Number may simplify a functional program. We can, for example, isolate parts of a complex algorithm to the Number subclass definition, making other parts of the application simpler or clearer.

Python already offers a rich variety of numeric types. The built-in types of the int and float variables cover a wide variety of problem domains. When working with currency, the decimal.Decimal package handles this elegantly. In some cases, we might find the fractions.Fraction class to be more appropriate than the float variable.

When working with geographic data, for example, we might consider creating a subclass of float variables that introduce additional attributes for conversion between degrees of latitude (or longitude) and radians. The arithmetic operations in this subclass could be done  to simplify calculations that move across the equator or the Greenwich meridian.

Since the Python Numbers class is intended to be immutable, ordinary functional design can be applied to all of the various method functions. The exceptional, Python, in-place, special methods (for example, the __iadd__() function) can be simply ignored.

When working with subclasses of Number, we have a fairly extensive volume of design considerations, as follows:

  • Equality testing and hash value calculation. The core features of a hash calculation for numbers is documented in the 9.1.2 Notes for type implementors section of the Python Standard Library.
  • The other comparison operators (often defined via the @total_ordering decorator).
  • The arithmetic operators—+, -, *, /, //, %, and **. There are special methods for the forward operations as well as additional methods for reverse type-matching. Given an expression such as a-b, Python uses the type of a to attempt to locate an implementation of the __sub__() method function, effectively, the a.__sub__(b) method. If the class of the left-hand value, a in this case, doesn't have the method or returns the NotImplemented exception, then the right-hand value is examined to see if the b.__rsub__(a) method provides a result. There's an additional special case that applies when class b is a subclass of class a this allows the subclass to override the left-hand side operation choice.
  • The bit-fiddling operators—&, |, ^, >>, <<, and ~. These might not make sense for floating-point values; omitting these special methods might be the best design.
  • Some additional functions like round(), pow(), and divmod() are implemented by numeric special method names. These might be meaningful for this class of numbers.

Chapter 7, Additional Tuple Techniques, provides a detailed example of creating a new type of number. Visit the link for more details: https://www.packtpub.com/application-development/mastering-object-oriented-python.

As we noted previously, functional programming and object-oriented programming can be complementary. We can easily define classes that follow functional programming design patterns. Adding new kinds of numbers is one example of leveraging Python's object-oriented features to create more readable functional programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset