Keeping a list of records with named fields that can be updated
Peter Otten
__peter__ at web.de
Mon Dec 19 12:42:43 EST 2022
On 17/12/2022 20:45, Albert-Jan Roskam wrote:
> On Dec 15, 2022 10:21, Peter Otten <__peter__ at web.de> wrote:
>
> >>> from collections import namedtuple
> >>> Row = namedtuple("Row", "foo bar baz")
> >>> row = Row(1, 2, 3)
> >>> row._replace(bar=42)
> Row(foo=1, bar=42, baz=3)
>
> ====
> Ahh, I always thought these are undocumented methods, but: "In addition to
> the methods inherited from tuples, named tuples support three additional
> methods and two attributes. To prevent conflicts with field names, the
> method and attribute names start with an underscore."
> https://docs.python.org/3/library/collections.html#collections.somenamedtuple._make
I've read somewhere that Raymond Hettinger regrets the naming and now
would favour a trailing underscore to avoid name conflicts.
More information about the Python-list
mailing list