13.11. OOP Method Parameters¶
Methods are functions in the class
Prevents copy-paste code
Improves readability
Improves refactoring
Decomposes bigger problem into smaller chunks
At definition -
self
should always be a first parameterAt call -
self
is not passed as an argument (Python will do that)Later you will learn more advanced things like static methods etc.
Required parameter:
Necessary to call that function
Specified at leftmost side
Optional parameter:
Has default value
Optional to call that function
Default value will be overridden if specified at a call time
Specified at rightmost side
- method¶
Functions in the class which takes instance as first argument (
self
)
13.11.1. Without Parameters¶
>>> class User:
... def login(self):
... print(f'User logged-in')
13.11.2. Required Parameters¶
At definition -
self
should always be a first parameterLater you will learn more advanced things like static methods etc.
Parameter - Receiving variable used within the function
Parameters could be required or optional (with default value)
>>> class User:
... def login(self, username, password):
... print(f'User logged-in')
13.11.3. Optional Parameters¶
>>> class User:
... def login(self, username=None, password=None):
... print(f'User logged-in')
13.11.4. Required and Optional Parameters¶
>>> class User:
... def login(self, username, password=None):
... print(f'User logged-in')
13.11.5. Use Case - 0x01¶
>>> class User:
... def login(self, username, password):
... if username == 'mwatney' and password == 'nasa':
... print('ok')
... else:
... raise PermissionError
...
>>>
>>>
>>> mark = User()
>>>
>>> mark.login()
Traceback (most recent call last):
TypeError: User.login() missing 2 required positional arguments: 'username' and 'password'
>>>
>>> mark.login('invalid', 'invalid')
Traceback (most recent call last):
PermissionError
>>>
>>> mark.login('mwatney', 'invalid')
Traceback (most recent call last):
PermissionError
>>>
>>> mark.login('invalid', 'nasa')
Traceback (most recent call last):
PermissionError
>>>
>>> mark.login('mwatney', 'nasa')
ok
>>>
>>> mark.login(username='mwatney', password='nasa')
ok
13.11.6. Assignments¶
"""
* Assignment: OOP Method Syntax
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min
English:
1. Define class `Calculator`
2. Define method `add` in class `Calculator`
3. Method `add` take `a` and `b` as arguments
4. Method returns sum of `a` and `b`
5. Run doctests - all must succeed
Polish:
1. Zdefiniuj klasę `Calculator`
2. Zdefiniuj metodę `add` w klasie `Calculator`
3. Metoda `add` przyjmuje `a` i `b` jako argumenty
4. Metoda zwraca sumę `a` i `b`
5. Uruchom doctesty - wszystkie muszą się powieść
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isclass, ismethod
>>> assert isclass(Calculator)
>>> calc = Calculator()
>>> assert ismethod(calc.add)
>>> calc.add(1, 2)
3
"""
"""
* Assignment: OOP Method Mean
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min
English:
1. Define class `Stats`
2. Define method `mean()` in `Stats` class
3. Method takes `data: list[float]` as an argument
4. Method returns arithmetic mean of the `data`
5. Returned value must be rounded to one decimal places
6. Run doctests - all must succeed
Polish:
1. Zdefiniuj klasę `Stats`
2. Zdefiniuj metodę `mean()` w klasie `Stats`
3. Metoda przyjmuje `data: list[float]` jako argument
4. Metoda zwraca średnią arytmetyczną z `data`
5. Zwracana wartość ma być zaokrąglona do jednego miejsca po przecinku
6. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `round()`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isclass, ismethod
>>> assert isclass(Stats)
>>> stats = Stats()
>>> assert ismethod(stats.mean)
>>> stats.mean([1, 2])
1.5
>>> stats.mean([5.8, 2.7, 5.1, 1.9])
3.9
"""
"""
* Assignment: OOP Method Call
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Iterate over `DATA` skipping header and separating features from labels
2. Call `mean()` method of `Stats` class instance passing list of features
3. Define `result: list[float]` with list of means from each row
4. Run doctests - all must succeed
Polish:
1. Iteruj po `DATA` pomijając nagłówek i rodzielając cechy od etykiet
2. Wywołuj metodę `mean()` instancji klasy `Stats` przekazując listę cech
3. Zdefiniuj `result: list[float]` z listą średnich każdego z wierszy
4. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `round()`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert type(result) is list
>>> assert all(type(x) is float for x in result)
>>> result
[3.9, 2.5, 3.5, 4.1, 3.9, 2.4]
"""
DATA = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
]
class Stats:
def mean(self, data):
avg = sum(data) / len(data)
return round(avg, 1)
stats = Stats()
# Apply `mean()` to each feature
# type: list[float]
result = ...