12.5. OOP Attribute Set

12.5.1. Syntax

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

12.5.2. Attributes and Instances

  • Dynamic attributes

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

12.5.3. List

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     missions: list[str]
>>>
>>> mark = Astronaut()
>>> mark.missions = ['Ares1', 'Ares3']

12.5.4. Union

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int | float
>>>
>>>
>>> mark = Astronaut()
>>> mark.age = 40
>>>
>>> melissa = Astronaut()
>>> melissa.age = 44.5

12.5.5. Optional

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

12.5.6. 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
>>>
>>>
>>> astro = Astronaut()
>>> astro.firstname = 'Mark'
>>> astro.lastname = 'Watney'
>>> astro.agency = 'NASA'
>>> astro.flown = True
>>> astro.age = 40
>>> astro.height = 185.5
>>> astro.weight = 75.5
>>> astro.missions = ['Ares1', 'Ares3']
>>> astro.friends = None

12.5.7. Use Case - 0x01

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

12.5.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

12.5.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'

12.5.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'

12.5.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'

12.5.12. Assignments

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

English:
    1. Modify code below
    2. Set attibutes of watney and nasa instances to model the data:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Ustaw atrybuty instancji watney i nasa by zamodelować dane:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isinstance(mark, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
    >>> assert 'Mark' in vars(mark).values()
    >>> assert 'USA' in vars(mark).values()
    >>> assert '1969-07-21' in vars(mark).values()
    >>> assert 'Nasa' in vars(nasa).values()
    >>> assert 'USA' in vars(nasa).values()
    >>> assert '1969-07-21' in vars(nasa).values()
"""


class Astronaut:
    name: str
    country: str
    date: str


class SpaceAgency:
    name: str
    country: str
    date: str


mark = Astronaut()
nasa = SpaceAgency()

# Mark, USA, 1969-07-21
# Nasa, USA, 1969-07-21