Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content