Getting named attributes when using higher-order functions

Let's look at a slightly different collection of data. Let's say we were working with NamedTuple subclasses instead of anonymous tuples. First, we'll define a class that has type hints for both items within the tuple:

from typing import NamedTupleclass YearCheese(NamedTuple):    year: int    cheese: float

Then, we can convert our base year_cheese data into properly named tuples. The conversion is shown, as follows:

>>> year_cheese_2 = list(YearCheese(*yc) for yc in year_cheese)>>> year_cheese_2
[YearCheese(year=2000, cheese=29.87),  YearCheese(year=2001, cheese=30.12), YearCheese(year=2002, cheese=30.6),  YearCheese(year=2003, cheese=30.66), YearCheese(year=2004, cheese=31.33),  YearCheese(year=2005, ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.