16.7. OOP Attribute Define

  • Attribute Annotation is optional, but a good practice

16.7.1. No Attribute Definition

>>> class User:
...     pass

16.7.2. Basic Types

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

16.7.3. Union

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

16.7.4. Optional

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

16.7.5. Sequences

  • Since Python 3.9 you can use list[str]

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

>>> class User:
...     firstname: str
...     lastname: str
...     age: int
...     groups: list[str]

16.7.6. Relation One to One

>>> class Group:
...     gid: int
...     name: int
>>>
>>>
>>> class User:
...     firstname: str
...     lastname: str
...     group: Group

16.7.7. Relation One to Many

>>> class Group:
...     gid: int
...     name: str
>>>
>>>
>>> class User:
...     firstname: str
...     lastname: str
...     groups: list[Group]

16.7.8. Example

>>> class User:
...     firstname: str
...     lastname: str
...     email: str
...     active: bool
...     age: int | float
...     height: float | None
...     weight: float | None
...     groups: list[str] | None
...     friends: list['User'] | None

16.7.9. Good Practices

  • snake_case name convention

  • Attributes should be defined only in __init__() method

  • More information in OOP Init Method

16.7.10. Use Case - 0x01

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

16.7.11. Use Case - 0x02

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

16.7.12. Use Case - 0x03

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

16.7.13. Use Case - 0x04

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

16.7.14. Use Case - 0x05

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

16.7.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
...     birthdate: 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