Friday, 13 July 2018

Adding fields to NamedTuple from previous fields

Let's say I want to store some information about a conference schedule with a presentation time and a pause time. I can do this in a NamedTuple.

from typing import NamedTuple

class BlockTime(NamedTuple):
    t_present: float
    t_pause: float

However, if I also want to store how much each block would take such that t_each = t_pause + t_present, I can't just add it as an attribute:

class BlockTime(NamedTuple):
    t_present: float
    t_pause: float
    # this causes an error
    t_each = t_present + t_pause

What is the correct way to do this in Python? If I make an __init__(self) method and store it as an instance variable there, but it would then be mutable.



from Adding fields to NamedTuple from previous fields

No comments:

Post a Comment