3. Methods

  • Methods are functions in the class

  • First argument is always instance (self)

  • While calling function you never pass self

3.1. Methods without arguments

Listing 244. Methods without arguments
class Iris:
    def latin_name(self):
        print(f'Latin name: Iris Setosa')


flower = Iris()

flower.latin_name()
# Latin name: Iris Setosa

3.2. Methods with argument

Listing 245. Methods with arguments
class Iris:
    def latin_name(self, species):
        print(f'Latin name: {species}')


flower = Iris()

flower.latin_name(species='Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name('Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name()
# TypeError: latin_name() missing 1 required positional argument: 'species'

3.3. Methods with arguments with default value

Listing 246. Methods with arguments with default value
class Iris:
    def latin_name(self, species='unknown'):
        print(f'Latin name: Iris {species}')


flower = Iris()

flower.latin_name(species='Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name('setosa')
# Latin name: Iris Setosa

flower.latin_name()
# Latin name: unknown

3.4. Methods calling other methods

Listing 247. Methods calling other methods
class Iris:
    def get_name(self):
        return 'Iris Setosa'

    def latin_name(self):
        name = self.get_name()
        return f'Latin name: {name}'


flower = Iris()

flower.latin_name()
# Latin name: Iris Setosa

3.5. Methods accessing fields

Listing 248. Methods accessing fields
class Iris:
    def __init__(self, species='unknown'):
        self.species = species

    def latin_name(self):
        print(f'Latin name is: {self.species}')


setosa = Iris('Iris Setosa')
setosa.latin_name()
# Latin name is: Iris Setosa

iris = Iris()
iris.latin_name()
# Latin name is: unknown

3.6. Assignments

3.6.1. Methods

  • Complexity level: easy

  • Lines of code to write: 15 lines

  • Estimated time of completion: 15 min

  • Filename: solution/syntax_methods.py

English
  1. Create class Iris

  2. Create method total() which sums all the numerical attributes values

  3. Create method mean() which calculates mean of numerical attributes values

  4. Create setosa object with attributes set at the initialization (see input data)

  5. Create virginica object with attributes set at the initialization (see input data)

  6. Print species name, total and mean of each instance

Polish
  1. Stwórz klasę Iris

  2. Napisz metodę total() wyliczającą sumę atrybutów numerycznych obiektu

  3. Napisz metodę mean() wyliczającą średnią atrybutów numerycznych obiektu

  4. Stwórz obiekt setosa z pomiarami podawanymi przy inicjalizacji (patrz dane wejściowe)

  5. Stwórz obiekt virginica z pomiarami podawanymi przy inicjalizacji (patrz dane wejściowe)

  6. Wypisz nazwę gatunku oraz sumę i średnią z pomiarów dla każdej instancji

Input
Listing 249. Setosa initial values
sepal_length = 5.4
sepal_width = 3.9
petal_length = 1.3
petal_width = 0.4
Listing 250. Virginica initial values
sepal_length = 5.8
sepal_width = 2.7
petal_length = 5.1
petal_width = 1.9