9.2. Methods

9.2.1. Rationale

  • Methods are functions in the class

  • First argument is always instance (self)

  • While calling function you never pass self

  • Prevents copy-paste code

  • Improves readability

  • Improves refactoring

  • Decomposes bigger problem into smaller chunks

method

Functions in the class which takes instance as first argument (self)

Listing 9.8. Syntax
class MyClass:
    def mymethod(self):
        ...

my = MyClass()
my.mymethod()

9.2.2. Methods Without Arguments

Listing 9.9. Methods without arguments
class Astronaut:
    def say_hello(self):
        print('My name... José Jiménez')


jose = Astronaut()
jose.say_hello()
# My name... José Jiménez

9.2.3. Methods With Required Argument

Listing 9.10. Methods with required argument
class Astronaut:
    def say_hello(self, name):
        print(f'My name... {name}')


jose = Astronaut()

jose.say_hello(name='José Jiménez')
# My name... José Jiménez

jose.say_hello('José Jiménez')
# My name... José Jiménez

jose.say_hello()
# Traceback (most recent call last):
#     ...
# TypeError: say_hello() missing 1 required positional argument: 'name'

9.2.4. Methods With Optional Argument

Listing 9.11. Methods with arguments with default value
class Astronaut:
    def say_hello(self, name='Unknown'):
        print(f'My name... {name}')


jose = Astronaut()

jose.say_hello(name='José Jiménez')
# My name... José Jiménez

jose.say_hello('José Jiménez')
# My name... José Jiménez

jose.say_hello()
# My name... Unknown

9.2.5. Assignments

9.2.5.1. OOP Method Sequence

  • Assignment name: OOP Method Sequence

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 18 lines

  • Estimated time of completion: 13 min

  • Solution: solution/oop_method_iris.py

English
  1. Use data from "Input" section (see below)

  2. Define class Iris

  3. Define method mean() in Iris class

  4. Method takes sequence as an argument

  5. Method must return arithmetic mean of the sequence

  6. Iterate over DATA omitting header

  7. Separate features from label

  8. Call mean() method of Iris class passing features as an argument

  9. Sum all mean results

  10. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Zdefiniuj klasę Iris

  3. Zdefiniuj metodę mean() w klasie Iris

  4. Metoda przyjmuje sekwencję jako argument

  5. Metoda ma zwracać średnią arytmetyczną z sekwencji

  6. Iteruj po DATA pomijając nagłówek

  7. Rozdziel features od label

  8. Wywołuj metodę mean() klasy Iris przekazując features jako argument

  9. Zsumuj wyniki wszystkich średnich

  10. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
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'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
    (4.9, 3.0, 1.4, 0.2, 'setosa'),
    (4.9, 2.5, 4.5, 1.7, 'virginica'),
    (7.1, 3.0, 5.9, 2.1, 'virginica'),
    (4.6, 3.4, 1.4, 0.3, 'setosa'),
    (5.4, 3.9, 1.7, 0.4, 'setosa'),
    (5.7, 2.8, 4.5, 1.3, 'versicolor'),
    (5.0, 3.6, 1.4, 0.3, 'setosa'),
    (5.5, 2.3, 4.0, 1.3, 'versicolor'),
    (6.5, 3.0, 5.8, 2.2, 'virginica'),
    (6.5, 2.8, 4.6, 1.5, 'versicolor'),
    (6.3, 3.3, 6.0, 2.5, 'virginica'),
    (6.9, 3.1, 4.9, 1.5, 'versicolor'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]
Output
result: float
# 73.39999999999999