Abstract
Describes an implementation of a class for representing rational numbers in the Python programming language.
This publication is available in Web form and also as a PDF document. Please
forward any comments to tcc-doc@nmt.edu.
Table of Contents
rational.py modulegcd() functionclass RationalRational.__init__(): The constructorRational.__add__(): Implement the
addition (+) operatorRational.__sub__(): Implement
subtractionRational.__mul__(): Implement
multiplicationRational.__div__(): Implement
divisionRational.__str__(): Convert a
rational to a stringRational.__float__(): Implement the
float() functionRational.mixed(): Display as a mixed
fractionrationaltest: A small test driver
This document describes a Python module for working with
rational numbers. It is intended as an example of a Python
class for students new to object-oriented
programming.
This publication assumes that the reader has had a
general introduction to the construction of Python
classes. In particular, you should know that the name of
the class's constructor method is always __init__. Method names such as __init__, which start and end with two underbar
(_) characters are called special methods.
This class makes heavy use of Python special methods to
implement the common mathematical operators such as + and -. For example, when you
have two instances and x of some class, using the y- (subtract)
operator invokes the special method __sub__(.
x, y)
Relevant online files:
Here are some links to related Python documentation:
Python tutorials: Basic training for the Python beginner.