A named tuple combines tuple immutability with named fields. It can be accessed by index or attribute, making structured program state clearer than positional tuples while avoiding mutable dictionary keys. section 22.1.1 section 22.1.2
Create and use a named tuple in three steps:
- Import namedtuple and define a record type: State = namedtuple('State', ['cell', 'player']). section 22.1.2
- Instantiate it with State(1, 'X'), then read state.cell, state.player, state[0], or state[1]. section 22.1.2
- Update it by assigning the result of state._replace(board=new_board, player='O') to a new or existing variable. section 22.1.5
Typed game state
A typing.NamedTuple class can describe a complete Tic-Tac-Toe state: class State(NamedTuple): board: List[str] = list('.' * 9); player: str = 'X'; quit: bool = False; draw: bool = False; error: Optional[str] = None; winner: Optional[str] = None. State() starts an empty game, while State(player='O') overrides one default. Type hints describe intended types, and mypy can detect misuse before runtime. section 22.1.3 section 22.4
Use the Tic-Tac-Toe state table to trace how an initially empty board changes across interactive turns. section 22.0



