June 2019
Beginner to intermediate
770 pages
19h 24m
English
The NamedTuple class expects a number of class-level attributes. These attributes will typically have type hints, and provide a way to give names to the attributes of a tuple.
Using a NamedTuple subclass can condense a class definition into a very short definition of a simple immutable object. It saves us from having to write longer and more complex class definitions for the common case where we want to name a fixed set of attributes.
For something like a playing card, we might want to insert the following code in a class definition:
from typing import NamedTupleclass BlackjackCard_T(NamedTuple): rank: str suit: Suit hard: int soft: int
We defined a new class and provided four named attributes: rank, suit, ...
Read now
Unlock full access