Writing Your Own Class Methods
In the lecture, we went through the basic construction of a Rational
class.
However, there is a lot of functionality left to implement. That is left up to you.
Download the example class, and get started.
class Rational:
"""An example Rational class.
This class attempts to implement rational numbers, as an *exercise*.
For actual use cases, use the rationals available from software such as sympy,
sage, or the builtin numbers.Rational class.
Attributes
----------
numerator : int
Numerator of the rational.
denominator : int
Denominator of the rational.
"""
def __init__(self,numerator, denominator):
"""Creates a new Rational from numerator and denominator.
Parameters
----------
numerator : int
Intended numerator of the rational.
denominator : int
Intended denominator of the rational.
"""
self.numerator = numerator
self.denominator = denominator
def __add__(self,other):
# Note that special "magic" methods (except __init__), by default, don't have docstrings.
# Their behavior should be apparent from the mathematics of an object-
# If not, feel free to explain in the class docsting.
if isinstance(other, Rational):
new_numerator = self.numerator*other.denominator+self.denominator*other.numerator
new_denominator = self.denominator*other.denominator
return Rational(new_numerator, new_denominator)
if isinstance(other, int):
new_numerator = self.numerator + other*self.denominator
return Rational(new_numerator,self.denominator)
raise NotImplementedError("Cannot Add Types {} and {}".format("Rational",type(other)))
def __neg__(self):
return Rational(-self.numerator, self.denominator)
def __sub__(self,other):
return self+(-other)
def __repr__(self):
return "Rational({numerator},{denominator})".format(**self.__dict__)
def __radd__(self,other):
return self+other
def __rsub__(self,other):
return -self+other
Graded Assignment: Extend the
Rational
class with the following behaviors:
- String representation as "a/b" for printing
- Initializing from Integers - e.g.
Rational(5)
- Left and Right Multiplication and Division by:
- Integers
- Rationals
- Integer Part - e.g.
int(Rational(5,2))
should return the integer2
.- Equality Checking (including
==
and!=
)- All 4 Inequality Checks
- Absolute Value
- Raising
ZeroDivisionError
whenever asked to divide by 0, in initialization or arithmeticUpload a
.py
file containing your class - and no other code - to Brightspace.
Additional Behaviors
Here is a list of some useful additional behaviors you can practice implementing:
- Automatic reducing of fractions (hint: use the statement
from math import gcd
to get a simplegcd
function in your file.) - Initialization from floating point numbers
- Conversion to floating point numbers
- An implementation to check if an integer, like the floating point
.is_integer
function - Exponentiation
- Modulo operator
%
- Truthiness with
__bool__
- investigate the truthiness of integers to see what behavior would be expected.
If you want even more of a challenge, create your own Gaussian
integer class, and your own GaussianRational
class - try to design behaviors so you can reuse as much as possible without redefining!