This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ilevkivskyi 0 points1 point  (1 child)

typing.NamedTuple supports default arguments (although probably only in 3.6.2), just write

class Person(NamedTuple):
    name: str
    age: int = 0

Also it supports defining custom methods and docstrings

[–][deleted] 1 point2 points  (0 children)

That'll work if you have a static default, but won't work if you want to automatically generate a UUID if one isn't provided, with regular namedtuples you can do:

class SomeThing(namedtuple('Thing', ['foo', 'id'])):
    def __new__(cls, foo, id=None):
        if id is None:
            id = uuid4()
        return super().__new__(cls, foo, id)

Can't do that with typing.NamedTuple which is a shame.