Keeping a list of records with named fields that can be updated

songbird songbird at anthive.com
Mon Dec 19 14:22:34 EST 2022


Peter Otten wrote:
...
> I recommend that you use a dataclass /instead/ of a namedtuple, not
> both. However, for a dataclass with the same fields in the same order as
> in your namedtuple the conversion is trivial:
>
> Create compatible namedtuple and dataclass types:
>
> >>> NTRow = namedtuple("NTRow", ["alpha", "beta", "gamma"])
> >>> DCRow = make_dataclass("DCRow", NTRow._fields)
>
> Build the namedtuple:
>
> >>> ntrow = NTRow(1, "two", 3.0)
> >>> ntrow
> NTRow(alpha=1, beta='two', gamma=3.0)
>
> Convert to dataclass:
>
> >>> dcrow = DCRow(*ntrow)
> >>> dcrow
> DCRow(alpha=1, beta='two', gamma=3.0)

  thanks, once i get the data in from the file i only have
to reference it, but for the rest of the code i can use
the dataclass instead and that will be easier to read than
dicts.  :)

  your help is appreciated.  :)


  songbird


More information about the Python-list mailing list