Skip to content

Commit 7992989

Browse files
committed
Fix arguments in Special Methods lecture
1 parent 3f60700 commit 7992989

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

11 Classes and Objects Intermediate/1 Special methods.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Fraction:
1717
self.denom = denom
1818

1919
def mul(self, other):
20-
return Fraction(self.numer * self.numer, self.denom * self.denom)
20+
return Fraction(self.numer * other.numer, self.denom * other.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__`):
@@ -29,7 +29,7 @@ class Fraction:
2929
self.denom = denom
3030

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

3535
Now we can write:
@@ -137,7 +137,7 @@ Some other special functions that are very useful are comparisons. Fractions can
137137
> return f"{self.numer}/{self.denom}"
138138
>
139139
> def __mul__(self, other):
140-
> return Fraction(self.numer * self.numer, self.denom * self.denom)
140+
> return Fraction(self.numer * other.numer, self.denom * other.denom)
141141
> ```
142142
>
143143
> 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.

0 commit comments

Comments
 (0)