16.8. OOP Attribute Set

16.8.1. Syntax

>>> class User:
...     firstname: str
...     lastname: str
>>>
>>>
>>> mark = User()
>>> mark.firstname = 'Mark'
>>> mark.lastname = 'Watney'

16.8.2. Attributes and Instances

  • Dynamic attributes

>>> class User:
...     firstname: str
...     lastname: str
>>>
>>>
>>> mark = User()
>>> mark.firstname = 'Mark'
>>> mark.lastname = 'Watney'
>>>
>>> melissa = User()
>>> melissa.firstname = 'Melissa'
>>> melissa.lastname = 'Lewis'
>>>
>>> rick = User()
>>> rick.firstname = 'Rick'
>>> rick.lastname = 'Martinez'

16.8.3. List

>>> class User:
...     firstname: str
...     lastname: str
...     groups: list[str]
>>>
>>> mark = User()
>>> mark.groups = ['user', 'staff', 'admin']

16.8.4. Union

>>> class User:
...     firstname: str
...     lastname: str
...     age: int | float
>>>
>>>
>>> mark = User()
>>> mark.age = 42
>>>
>>> melissa = User()
>>> melissa.age = 41.5

16.8.5. Optional

>>> class User:
...     firstname: str
...     lastname: str
...     age: int | None
>>>
>>>
>>> mark = User()
>>> mark.age = 42
>>>
>>> melissa = User()
>>> melissa.age = None

16.8.6. 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
>>>
>>>
>>> mark = User()
>>> mark.firstname = 'Mark'
>>> mark.lastname = 'Watney'
>>> mark.email = 'mwatney@nasa.gov'
>>> mark.active = True
>>> mark.age = 42
>>> mark.height = 178.0
>>> mark.weight = 75.5
>>> mark.groups = ['user', 'staff', 'admin']
>>> mark.friends = None

16.8.7. Use Case - 0x01

>>> class Point:
...     x: int
...     y: int
...     z: int
>>> pt = Point()
>>> pt.x = 1
>>> pt.y = 2
>>> pt.z = 0

16.8.8. Use Case - 0x02

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

First man in space:

>>> gagarin = Date()
>>> gagarin.year = 1961
>>> gagarin.month = 4
>>> gagarin.day = 12

First man on the Moon:

>>> armstrong = Date()
>>> armstrong.year = 1969
>>> armstrong.month = 7
>>> armstrong.day = 21

16.8.9. Use Case - 0x03

>>> class Laptop:
...     cpu: str
...     ram: str
...     ssd: str
>>> macbook = Laptop()
>>> macbook.cpu = '2.9 GHz 6-Core Intel Core i9'
>>> macbook.ram = '32 GB 2400 MHz DDR4'
>>> macbook.ssd = '1 TB APPLE SSD AP1024M'

16.8.10. Use Case - 0x04

>>> class Iris:
...     features: list[float]
...     label: str
>>> setosa = Iris()
>>> setosa.features = [5.1, 3.5, 1.4, 0.2]
>>> setosa.label = 'setosa'

16.8.11. Use Case - 0x05

>>> class Iris:
...     sepal_length: float
...     sepal_width: float
...     petal_length: float
...     petal_width: float
...     species: str
>>> flower = Iris()
>>> flower.sepal_length = 5.1
>>> flower.sepal_width = 3.5
>>> flower.petal_length = 1.4
>>> flower.petal_width = 0.2
>>> flower.species = 'setosa'