12.4. OOP Attribute Define

  • Attribute Annotation is optional, but a good practice

12.4.1. No Attribute Definition

>>> class Astronaut:
...     pass

12.4.2. Basic Types

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int

12.4.3. Union

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int | float

12.4.4. Optional

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int | float
...     height: float | None
...     weight: float | None

12.4.5. Sequences

  • Since Python 3.9 you can use list[str]

  • Before Python 3.9 use list without specifying type of elements inside

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int
...     missions: list[str]

12.4.6. Relation One to One

>>> class Date:
...     year: int
...     month: int
...     day: int
>>>
>>>
>>> class Astronaut:
...     firstname: str
...     lastname: str
...     born: Date

12.4.7. Relation One to Many

>>> class Mission:
...     year: int
...     name: str
>>>
>>>
>>> class Astronaut:
...     firstname: str
...     lastname: str
...     missions: list[Mission]

12.4.8. Example

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     agency: str
...     flown: bool
...     age: int | float
...     height: float | None
...     weight: float | None
...     missions: list[str] | None
...     friends: list['Astronaut'] | None

12.4.9. Good Practices

  • snake_case name convention

  • Attributes should be defined only in __init__() method

  • More information in OOP Init Method

12.4.10. Use Case - 0x01

>>> class Point:
...     x: int
...     y: int
...     z: int

12.4.11. Use Case - 0x02

>>> class Date:
...     year: int
...     month: int
...     day: int

12.4.12. Use Case - 0x03

>>> class Laptop:
...     cpu: str
...     ram: str
...     ssd: str

12.4.13. Use Case - 0x04

>>> class Iris:
...     features: list[float]
...     label: str

12.4.14. Use Case - 0x05

>>> class Iris:
...     sepal_length: float
...     sepal_width: float
...     petal_length: float
...     petal_width: float
...     species: str

12.4.15. Use Case - 0x06

>>> from datetime import date
>>> from typing import Literal
>>>
>>>
>>> class Address:
...     type: Literal['home', 'work']
...     street: str
...     house: str
...     apartment: str
...     post_code: str
...     city: str
...     region: str
...     country: str
>>>
>>>
>>> class PhoneNumber:
...     type: Literal['home', 'work', 'mobile']
...     number: str
>>>
>>>
>>> class Person:
...     firstname: str
...     lastname: str
...     age: int | float
...     born: date
...     gender: Literal['male', 'female']
...     height: float | None
...     weight: float | None
...     education: list[str] | None
...     job: str | None
...     addresses: list[Address] | None
...     emails: list[str] | None
...     phones: PhoneNumber | None
...     friends: list['Person'] | None

12.4.16. Assignments

Code 12.5. Solution
"""
* Assignment: OOP Attribute Define
* Required: yes
* Complexity: easy
* Lines of code: 6 lines
* Time: 5 min

English:
    1. Modify code below
    2. Add type annotation attibutes to model the data:
       a. Watney, USA, 1969-07-21
       b. NASA, USA, 1958-07-29
    3. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Dodaj anotację typów atrybutów by zamodelować dane:
       a. Watney, USA, 1969-07-21
       b. NASA, USA, 1958-07-29
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert len(Astronaut.__annotations__) == 3
    >>> assert len(SpaceAgency.__annotations__) == 3
"""


# Watney, USA, 1969-07-21
# NASA, USA, 1958-07-29

class Astronaut:
    ...


class SpaceAgency:
    ...