6.1. OOP Principles

6.1.1. Rationale

  • Encapsulation

  • Abstraction

  • Inheritance

  • Polymorphism

6.1.2. Encapsulation

  • Do not allow to modify attributes directly

  • Hide them behind setters and getters

  • Prevents objects to get to an invalid state

position_x = 0
position_y = 0

def position_set(x, y):
    global position_x
    global position_y
    position_x = x
    position_y = y

def position_get():
    return position_x, position_y

position_set(1, 2)
position_get()
# (1, 2)
class Position:
    x: int = 0
    y: int = 0

    def set(self, x, y):
        self.x = x
        self.y = y

    def get(self):
        return self.x, self.y

position = Position()
position.set(1, 2)
position.get()
# (1, 2)

6.1.3. Abstraction

  • Reduce complexity by hiding unnecessary details

  • User do not need what does it mean to send email, that you have to connect, auth and later disconnect

class MailService:
    def send_email(sender, rcpt, subject, body):
        self._connect()
        self._authenticate()
        self._send(sender, rcpt, subject, body)
        self._disconnect()

    def _connect(self, timeout=1):
        print('Connect')

    def _authenticate(self):
        print('Authenticate')

    def _send(sender, rcpt, subject, body):
        print('Sending')

    def _disconnect(self):
        print('Disconnect')


if __name__ == '__main__':
    ms = MailService()
    ms.send_email(...)

6.1.4. Inheritance

class Person:
    _firstname: str
    _lastname: str

class Astronaut(Person):
    pass

6.1.5. Polymorphism

  • Ability of an object to take many forms

class Astronaut:
    def __init__(self, name):
        self.name = name

    def say_hello(self):
        return f'Hello {self.name}'


class Cosmonaut:
    def __init__(self, name):
        self.name = name

    def say_hello(self):
        return f'Привет {self.name}!'


if __name__ == '__main__':
    crew = [Astronaut('Mark Watney'),
            Cosmonaut('Иван Иванович'),
            Astronaut('Melissa Lewis'),
            Cosmonaut('Jan Twardowski')]

    for member in crew:
        print(member.say_hello())
# Hello Mark Watney
# Привет Иван Иванович
# Hello Melissa Lewis
# Привет Jan Twardowski