10.6. OOP Inheritance

10.6.1. Simple inheritance

class Iris:
    def __init__(self, sepal_length, sepal_width,
                 petal_length, petal_width, species):

        self.sepal_length = sepal_length
        self.sepal_width = sepal_width
        self.petal_length = petal_length
        self.petal_width = petal_width
        self.species = species


class Setosa(Iris):
    pass


setosa = Setosa(
    sepal_length=5.1,
    sepal_width=3.5,
    petal_length=1.4,
    petal_width=0.2,
    species='setosa'
)

10.6.2. Multilevel Inheritance

Listing 118. Multilevel Inheritance
class Flower:
    kingdom = 'plantae'

class Iris(Flower):
    genus = 'iris'

class Setosa(Iris):
    species = 'setosa'


setosa = Setosa()

setosa.species     # setosa
setosa.genus       # iris
setosa.kingdom     # plantae

10.6.3. Multiple Inheritance

class Flower:
    kingdom = 'plantae'

class Iris:
    genus = 'iris'

class Setosa(Flower, Iris):
    species = 'setosa'


setosa = Setosa()

setosa.species     # setosa
setosa.genus       # iris
setosa.kingdom     # plantae

10.6.4. Calling parent methods

Listing 119. Using super() on a class
class Iris:
    def __init__(self):
        self.sepal_length=5.1
        self.sepal_width=3.5
        self.petal_length=1.4
        self.petal_width=0.2


class Setosa(Iris):
    def __init__(self):
        super().__init__()
        self.species = 'setosa'


flower = Setosa()

flower.sepal_length     # 5.1
flower.sepal_width      # 3.4
flower.petal_length     # 1.4
flower.petal_width      # 0.2
flower.species          # setosa

10.6.5. Assignments

10.6.5.1. Objects and relations

English
  1. Client can open a bank account

  2. Client can have many accounts

  3. Bank has many clients

  4. Each account has unique number generated when opening an account

  5. Client can ask about number of all of his accounts

  6. Client can add money to the account

  7. Client can withdraw money from the account

Polish
  1. Klient może otworzyć konto w banku

  2. Klient może mieć wiele kont

  3. Bank może mieć wielu klientów

  4. Każde konto ma unikalny numer, który jest generowany przy zakładaniu

  5. Klient może odpytać o numery wszystkich swoich kont

  6. Klient może wpłacić pieniądze na swoje konto

  7. Klient może wybrać pieniądze z bankomatu