Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's great that we have simple/clean declarations for NamedTuples an (Data)classes now. But I wonder why they chose two different styles for creating them. This for NamedTuples:

    from typing import NamedTuple

    class Foo(NamedTuple):
        bar: str
        baz: int
and this for DataClasses:

    @dataclass
    class Foo:
        bar: str
        baz: int


When you write it that way it makes me wonder why there isn't a DataClass type


The short answer is that the only way to do what dataclasses do as a base class is via python metaclasses, and you can only have one metaclass. So this way, you can dataclassify something that inherits from a metaclass.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: