Skip to content

Commit 3f60700

Browse files
committed
Fix fractions terminology
1 parent 8ddf076 commit 3f60700

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

09 Classes and Objects Basis/3 Custom data types.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,32 @@ name = "python"
1313
print(name.upper()) # `upper()` is a method in `str` class
1414
```
1515

16-
You may define any type you want. Consider a class defining a fraction. It should have two attributes, *nominator* (named shortly `nom`) and *denominator* (`denom`):
16+
You may define any type you want. Consider a class defining a fraction. It should have two attributes, *numerator* (named shortly `numer`) and *denominator* (`denom`):
1717

1818
```python
1919
class Fraction:
20-
def __init__(self, nom, denom):
21-
self.nom = nom
20+
def __init__(self, numer, denom):
21+
self.numer = numer
2222
self.denom = denom
2323
```
2424

2525
It is possible to perform mathematical operations on fractions, like addition, subtraction, multiplication and division. Let's define a function `mul` that takes two fractions, multiplies them, and returns a `Fraction` instance, which is their factor:
2626

2727
```python
2828
def mul(a, b):
29-
return Fraction(a.nom * b.nom, a.denom * b.denom)
29+
return Fraction(a.numer * b.numer, a.denom * b.denom)
3030
```
3131

3232
Actually this function is specific for fractions only. So, in order to keep things together, it is better to make it a method in a fraction:
3333

3434
```python
3535
class Fraction:
36-
def __init__(self, nom, denom):
37-
self.nom = nom
36+
def __init__(self, numer, denom):
37+
self.numer = numer
3838
self.denom = denom
3939

4040
def mul(self, other):
41-
return Fraction(self.nom * self.nom, self.denom * self.denom)
41+
return Fraction(self.numer * self.numer, self.denom * self.denom)
4242
```
4343

4444
We may use this method as follows:

11 Classes and Objects Intermediate/1 Special methods.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ Let's go back to our [`Fraction` class](../09%20Classes%20and%20Objects%20Basis/
1212

1313
```python
1414
class Fraction:
15-
def __init__(self, nom, denom):
16-
self.nom = nom
15+
def __init__(self, numer, denom):
16+
self.numer = numer
1717
self.denom = denom
1818

1919
def mul(self, other):
20-
return Fraction(self.nom * self.nom, self.denom * self.denom)
20+
return Fraction(self.numer * self.numer, self.denom * self.denom)
2121
```
2222

2323
Such formulation allows us to multiply two fractions in an ugly way `c = a.mul(b)`. It would be much more clear if we were able to write `c = a * b`. In fact we can. Let's rename the method `mul` to `__mul__` (two underscored in the beginning and the end, like in `__init__`):
2424

2525
```python
2626
class Fraction:
27-
def __init__(self, nom, denom):
28-
self.nom = nom
27+
def __init__(self, numer, denom):
28+
self.numer = numer
2929
self.denom = denom
3030

3131
def __mul__(self, other):
32-
return Fraction(self.nom * self.nom, self.denom * self.denom)
32+
return Fraction(self.numer * self.numer, self.denom * self.denom)
3333
```
3434

3535
Now we can write:
@@ -65,8 +65,8 @@ An important special method you should always implement is `__str__`. Consider t
6565

6666
```python
6767
class Fraction:
68-
def __init__(self, nom, denom):
69-
self.nom = nom
68+
def __init__(self, numer, denom):
69+
self.numer = numer
7070
self.denom = denom
7171

7272
frac = Fraction(1, 2)
@@ -84,13 +84,13 @@ This is not very useful. Luckily, the `__str__` method can be used to convert th
8484

8585
```python
8686
class Fraction:
87-
def __init__(self, nom, denom):
88-
self.nom = nom
87+
def __init__(self, numer, denom):
88+
self.numer = numer
8989
self.denom = denom
9090

9191
def __str__(self):
9292
# This method must RETURN a string. Do not try to print anything!
93-
return f"{self.nom}/{self.denom}"
93+
return f"{self.numer}/{self.denom}"
9494

9595
frac = Fraction(1, 2)
9696

@@ -105,7 +105,7 @@ Our fraction is: 1/2
105105

106106
## Object comparison
107107

108-
Some other special functions that are very useful are comparisons. Fractions can be equal (if both nominators and denominators equal) or one can be larger than another one. Special functions doing comparisons should always return `True` or `False`. Their names are as follow:
108+
Some other special functions that are very useful are comparisons. Fractions can be equal (if both numerators and denominators equal) or one can be larger than another one. Special functions doing comparisons should always return `True` or `False`. Their names are as follow:
109109

110110
| Comparison | Special function |
111111
| ---------- | ------------------- |
@@ -122,25 +122,25 @@ Some other special functions that are very useful are comparisons. Fractions can
122122
>
123123
> When you implement arithmetic operation on fractions, you may end up with a reducible fraction, e.g. 2/3 × 3/5 = 6/15. Naturally, this fraction is equal to 2/5.
124124
>
125-
> In order to have your class behave elegantly and for comparisons to work, you should always reduce the fraction by dividing both the nominator and denominator by their [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor), which can be found using the function `gcd` in the `math` module. In which method you should do it? If you do this in every special method responsible for mathematical operation, you will need to do it several times. Furthermore, if someone creates an instance of you class e.g. as `Fraction(4, 6)`, the fraction will not be reduced. However if you make the reduction in the constructor, this will cover all use cases:
125+
> In order to have your class behave elegantly and for comparisons to work, you should always reduce the fraction by dividing both the numerator and denominator by their [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor), which can be found using the function `gcd` in the `math` module. In which method you should do it? If you do this in every special method responsible for mathematical operation, you will need to do it several times. Furthermore, if someone creates an instance of you class e.g. as `Fraction(4, 6)`, the fraction will not be reduced. However if you make the reduction in the constructor, this will cover all use cases:
126126
>
127127
> ```python
128128
> from math import gcd
129129
>
130130
> class Fraction:
131-
> def __init__(self, nom, denom):
132-
> r = gcd(nom, denom)
133-
> self.nom = nom // r # we use // for integer division
131+
> def __init__(self, numer, denom):
132+
> r = gcd(numer, denom)
133+
> self.numer = numer // r # we use // for integer division
134134
> self.denom = denom // r
135135
>
136136
> def __str__(self):
137-
> return f"{self.nom}/{self.denom}"
137+
> return f"{self.numer}/{self.denom}"
138138
>
139139
> def __mul__(self, other):
140-
> return Fraction(self.nom * self.nom, self.denom * self.denom)
140+
> return Fraction(self.numer * self.numer, self.denom * self.denom)
141141
> ```
142142
>
143-
> Also in the constructor you can do more sanitizations, like checking if the denominator is not 0 (and raising `ValueError` in such case), making sure that the denominator is always positive (and the sign of the nominator is adjusted accordingly), etc.
143+
> Also in the constructor you can do more sanitizations, like checking if the denominator is not 0 (and raising `ValueError` in such case), making sure that the denominator is always positive (and the sign of the numerator is adjusted accordingly), etc.
144144
145145
<hr/>
146146

0 commit comments

Comments
 (0)