12.6. OOP Attribute Get

12.6.1. Attribute Access

>>> class Astronaut:
...     firstname: str
...     lastname: str
>>>
>>>
>>> mark = Astronaut()
>>> mark.firstname = 'Mark'
>>> mark.lastname = 'Watney'
>>>
>>> print(f'Hello {mark.firstname} {mark.lastname}')
Hello Mark Watney

12.6.2. Accessing not Existing Attributes

>>> class Astronaut:
...     firstname: str
...     lastname: str
>>>
>>>
>>> astro = Astronaut()
>>> astro.firstname = 'Mark'
>>> astro.lastname = 'Watney'
>>>
>>> print(astro.missions)
Traceback (most recent call last):
AttributeError: 'Astronaut' object has no attribute 'missions'

12.6.3. Get All Attributes and Values

  • vars(obj) - returns all fields in dict format

Define a class

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     mission: str
...     agency: str

Set all attributes:

>>> watney = Astronaut()
>>> watney.firstname = 'Mark'
>>> watney.lastname = 'Watney'
>>> watney.mission = 'Ares 3'
>>> watney.agency = 'NASA'

Getting dynamic fields and values:

>>> vars(watney)  
{'firstname': 'Mark',
 'lastname': 'Watney',
 'mission': 'Ares 3',
 'agency': 'NASA'}

12.6.4. Select Attributes

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     age: int
...     height: float
...     weight: float
>>>
>>>
>>> astro = Astronaut()
>>> astro.firstname = 'Mark'
>>> astro.lastname = 'Watney'
>>> astro.age = 44
>>> astro.height = 185.5
>>> astro.weight = 75.5
>>>
>>> vars(astro)
{'firstname': 'Mark', 'lastname': 'Watney', 'age': 44, 'height': 185.5, 'weight': 75.5}
>>>
>>> list(vars(astro).values())
['Mark', 'Watney', 44, 185.5, 75.5]
>>>
>>> [x for x in vars(astro).values() if type(x) is str]
['Mark', 'Watney']
>>>
>>> [x for x in vars(astro).values() if type(x) in (float, int)]
[44, 185.5, 75.5]
>>>
>>> {k:v for k,v in vars(astro).items()}
{'firstname': 'Mark', 'lastname': 'Watney', 'age': 44, 'height': 185.5, 'weight': 75.5}
>>>
>>> {k:v for k,v in vars(astro).items() if k in ['firstname', 'lastname']}
{'firstname': 'Mark', 'lastname': 'Watney'}
>>>
>>> {k:v for k,v in vars(astro).items() if type(v) is str}
{'firstname': 'Mark', 'lastname': 'Watney'}

12.6.5. Use Case - 0x01

>>> 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'
>>>
>>> vars(flower)  
{'sepal_length': 5.1,
 'sepal_width': 3.5,
 'petal_length': 1.4,
 'petal_width': 0.2,
 'species': 'setosa'}

12.6.6. Use Case - 0x02

>>> class Astronaut:
...     firstname: str
...     lastname: str
...     mission: str
...     agency: str
>>>
>>>
>>> watney = Astronaut()
>>> watney.firstname = 'Mark'
>>> watney.lastname = 'Watney'
>>> watney.mission = 'Ares 3'
>>> watney.agency = 'NASA'
>>>
>>> lewis = Astronaut()
>>> lewis.firstname = 'Melissa'
>>> lewis.lastname = 'Lewis'
>>> lewis.mission = 'Ares 3'
>>> lewis.agency = 'NASA'
>>>
>>> vars(watney)  
{'firstname': 'Mark',
 'lastname': 'Watney',
 'mission': 'Ares 3',
 'agency': 'NASA'}
>>>
>>> vars(lewis)  
{'firstname': 'Melissa',
 'lastname': 'Lewis',
 'mission': 'Ares 3',
 'agency': 'NASA'}

12.6.7. Assignments

Code 12.7. Solution
"""
* Assignment: OOP Attribute Set
* Required: yes
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Define `result_mark: dict[str,str]`
       with all attributes and values of `mark` object
    2. Define `result_nasa: dict[str,str]`
       with all attributes and values of `nasa` object
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result_mark: dict[str,str]`
       z wszystkimi atrybutami i wartościami obiektu `mark`
    2. Zdefiniuj `result_nasa: dict[str,str]`
       z wszystkimi atrybutami i wartościami obiektu `nasa`
    3. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert type(result_mark) is dict
    >>> assert type(result_nasa) is dict

    >>> result_mark
    {'name': 'Mark', 'country': 'USA', 'date': '1969-07-21'}

    >>> result_nasa
    {'name': 'Nasa', 'country': 'USA', 'date': '1969-07-21'}
"""


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


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


mark = Astronaut()
nasa = SpaceAgency()

mark.name = 'Mark'
mark.country = 'USA'
mark.date = '1969-07-21'

nasa.name = 'Nasa'
nasa.country = 'USA'
nasa.date = '1969-07-21'


# Dict with all attributes and values of `mark` object
# type: dict[str,str]
result_mark = ...

# Dict with all attributes and values of `nasa` object
# type: dict[str,str]
result_nasa = ...