class Name(NamedTuple):
first_name: str
last_name: str
name: Name = Name(first_name="Guido", last_name="Rossum")
updated = {**name._asdict()}
updated.update({"last_name": "Fox"})
updated_name: Name = Name(**updated)
I am aware the nature of tuples is meant to be immutable, but I can't think of any data objects which provide the conciseness of a NamedTuple.
(For example, if I want to define the keys and values of a JSON object, I can simply define the attributes as shown in Name
)
But let's say I need to update an attribute before I save to database, then being able to do that in a single line would be useful.