On 17 May 2017 at 18:38, Stephan Houben <[email protected]> wrote:
> Hi Sven,
>
> > But even given that (and I am only speaking for my team), I haven't even
> > seen a use-case for namedtuples in a year. Every time we considered it,
> > people said: "please make it its own class for documentary purposes; this
> > thing will tend to grow faster than we can imagine".
>
> Using namedtuple doesn't stop the class from being its "own class".
> Typical use case:
>
> class Foo(namedtuple("Foo", "bar "baz"), FooBase):
> "Foo is a very important class and you should totally use it."""
>
> def grand_total(self):
> return self.bar + self.baz
>
And the right (modern) way to do this is
from typing import NamedTuple
class Foo(NamedTuple):
"""Foo is a very important class and
you should totally use it.
"""
bar: int
baz: int = 0
def grand_total(self):
return self.bar + self.baz
typing.NamedTuple supports docstrings, user-defined methods, and default
values.
--
Ivan
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/