You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 09 Classes and Objects Basis/3 Custom data types.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,32 +13,32 @@ name = "python"
13
13
print(name.upper()) # `upper()` is a method in `str` class
14
14
```
15
15
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`):
17
17
18
18
```python
19
19
classFraction:
20
-
def__init__(self, nom, denom):
21
-
self.nom=nom
20
+
def__init__(self, numer, denom):
21
+
self.numer=numer
22
22
self.denom = denom
23
23
```
24
24
25
25
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:
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__`):
@@ -65,8 +65,8 @@ An important special method you should always implement is `__str__`. Consider t
65
65
66
66
```python
67
67
classFraction:
68
-
def__init__(self, nom, denom):
69
-
self.nom=nom
68
+
def__init__(self, numer, denom):
69
+
self.numer=numer
70
70
self.denom = denom
71
71
72
72
frac = Fraction(1, 2)
@@ -84,13 +84,13 @@ This is not very useful. Luckily, the `__str__` method can be used to convert th
84
84
85
85
```python
86
86
classFraction:
87
-
def__init__(self, nom, denom):
88
-
self.nom=nom
87
+
def__init__(self, numer, denom):
88
+
self.numer=numer
89
89
self.denom = denom
90
90
91
91
def__str__(self):
92
92
# This method must RETURN a string. Do not try to print anything!
93
-
returnf"{self.nom}/{self.denom}"
93
+
returnf"{self.numer}/{self.denom}"
94
94
95
95
frac = Fraction(1, 2)
96
96
@@ -105,7 +105,7 @@ Our fraction is: 1/2
105
105
106
106
## Object comparison
107
107
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:
109
109
110
110
| Comparison | Special function |
111
111
| ---------- | ------------------- |
@@ -122,25 +122,25 @@ Some other special functions that are very useful are comparisons. Fractions can
122
122
>
123
123
> 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.
124
124
>
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:
126
126
>
127
127
> ```python
128
128
>from math import gcd
129
129
>
130
130
>classFraction:
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
> Also in the constructor you can do more sanitizations, like checking if the denominator isnot0 (and raising `ValueError`in such case), making sure that the denominator is always positive (and the sign of the nominatoris adjusted accordingly), etc.
143
+
> Also in the constructor you can do more sanitizations, like checking if the denominator isnot0 (and raising `ValueError`in such case), making sure that the denominator is always positive (and the sign of the numeratoris adjusted accordingly), etc.
0 commit comments