14. Polymorphism

14.1. Switch

Listing 306. Switch moves business logic to the execution place
agency = 'NASA'

if agency == 'NASA':
    print('Howdy from NASA')
elif agency == 'Roscosmos':
    print('Privyet z Roscosmos')
elif agency == 'ESA':
    print('Guten Tag aus ESA')
else:
    raise NotImplementedError

14.2. Polymorphism on Function

Listing 307. Polymorphism on Function
class Astronaut:
    def say_hello(self):
        print('Howdy from NASA')

class Cosmonaut:
    def say_hello(self):
        print('Privyet z Roscosmos')


def hello(spaceman):
    spaceman.say_hello()


watney = Astronaut()
ivanovic = Cosmonaut()


hello(watney)
# Howdy from NASA

hello(ivanovic)
# Privyet z Roscosmos

14.3. Polymorphism on Classes

Listing 308. Polymorphism on Classes
class Spaceman:
    def __init__(self, name):
        self.name = name

    def say_hello(self):
        raise NotImplementedError


class Astronaut(Spaceman):
    def say_hello(self):
        print(f'Howdy from NASA')


class Cosmonaut(Spaceman):
    def say_hello(self):
        print(f'Privyet z Roscosmos')


crew = [
    Astronaut('Mark Watney'),
    Cosmonaut('Иван Иванович'),
    Astronaut('Matt Kowalski'),
    Cosmonaut('Jan Twardowski'),
]

for member in crew:
    member.say_hello()
    # Howdy from NASA
    # Privyet z Roscosmos
    # Howdy from NASA
    # Privyet z Roscosmos