= Noom(5)
a = Noom(6) b
Noom’s source
noom is python library that defines Noom datatype - an alternative to number type but with limited capabilities. This is developed as part of a learning exercise in literate programmimg - using nbdev and fastcore. The goal here is to create a
noom
class whose objects are compatible with common arithmetic (+, -, x, /, //) and comparison(=, < , >, <=, >=) operators.
Noom
Noom (val)
Noom is a number datatype alternative with limited capabilities.
Noom.__add__
Noom.__add__ (oth)
Noom.__sub__
Noom.__sub__ (oth)
Noom.__mul__
Noom.__mul__ (oth)
Noom.__truediv__
Noom.__truediv__ (oth)
Noom.__floordiv__
Noom.__floordiv__ (oth)
Now, we have defined the arithmetic operations for noom objects using Python’s magic methods. Let’s take a look at few examples:
+ b a
11
- b a
-1
* b a
30
/ b a
0.8333333333333334
// a b
1
Noom.__eq__
Noom.__eq__ (oth)
Return self==value.
Let’s define a few nooms and look at examples…
= Noom(5)
p = Noom(5)
q = Noom(6) r
Here are a couple of example to check equality of noom objects:
== q, p != q p
(True, False)
== r, p != r p
(False, True)
Equality check can be done for Noom objects only after __eq__
is defined. So now, we can add tests for the arithmetic operators as weel.
+ b, Noom(11)) test_eq(a
We need not define __ne__
, it is auto inferred by python once __eq__
is defined. This is valid for other comparison operators too.
+ b, Noom(12)) test_ne(a
Noom.__lt__
Noom.__lt__ (oth)
Return self<value.
Examples:
< q, p > q p
(False, False)
< r, p > r p
(True, False)
Noom.__le__
Noom.__le__ (oth)
Return self<=value.
Examples:
<= q, p >= q p
(True, True)
<= r, p >= r p
(True, False)