Live data from Hacker News

Guide to Data Classes in Python 3.7

realpython.com

1–10 of 48 posts

Re: Guide to Data Classes in Python 3.7

#2
I am happy that lightweight data classes are now in the stdlib -- hopefully this puts an expiration date on overuse of namedtuple, which has turned out to be kind of a bad idea. Being able to make sane lightweight classes in one-off one-file python scripts will be nice.

That said, on any package that I write that has third party dependencies anyway (read: all of them), I don't see a compelling reason to change from attrs.

Re: Guide to Data Classes in Python 3.7

#4
Data classes was an interesting read and will definitely save some time in the future, but I think the __slots__ mention is where I got the most value from (big memory and access speed savings!).

Additional reading on __slots__: https://stackoverflow.com/questions/472000/usage-of-slots

Re: Guide to Data Classes in Python 3.7

#5
The problem with dataclasses are:

1) they don’t support __slots__ and default values

2) type hints aren’t supported yet, and it doesn’t appear they’ll be added to the 3.6 backport

The former issue will be resolved (maybe in 3.8?) but it will require either a metaclass approach or the @dataclass decorator to build a separate class.

The good news is that dataclasses (the backport) work on PyPy 6.0.0.

Re: Guide to Data Classes in Python 3.7

#6
post #5

The problem with dataclasses are: 1) they don’t support __slots__ and default values 2) type hints aren’t supported yet, and it doesn’t appear they’ll be added to the 3.6 backport The former issue will be resolved (maybe in 3.8?) but it will require either a metaclass approach or the @dataclass decorator to build a separate class. The good news is that dataclasses (the backport) work on PyPy 6.0.0.

On the __slots__, the article claims that these do: https://realpython.com/python-data-classes/#optimizing-data-...

EDIT: From PEP 557: https://www.python.org/dev/peps/pep-0557/#support-for-automa...

“Support for automatically setting __slots__?

At least for the initial release, __slots__ will not be supported. __slots__ needs to be added at class creation time. The Data Class decorator is called after the class is created, so in order to add __slots__ the decorator would have to create a new class, set __slots__, and return it. Because this behavior is somewhat surprising, the initial version of Data Classes will not support automatically setting __slots__. There are a number of workarounds:

- Manually add __slots__ in the class definition.

- Write a function (which could be used as a decorator) that inspects the class using fields() and creates a new class with __slots__ set.”

Re: Guide to Data Classes in Python 3.7

#7
post #6
post #5

The problem with dataclasses are: 1) they don’t support __slots__ and default values 2) type hints aren’t supported yet, and it doesn’t appear they’ll be added to the 3.6 backport The former issue will be resolved (maybe in 3.8?) but it will require either a metaclass approach or the @dataclass decorator to build a separate class. The good news is that dataclasses (the backport) work on PyPy 6.0.0.

On the __slots__, the article claims that these do: https://realpython.com/python-data-classes/#optimizing-data-... EDIT: From PEP 557: https://www.python.org/dev/peps/pep-0557/#support-for-automa... “Support for automatically setting __slots__? At least for the initial release, __slots__ will not be supported. __slots__ needs to be added at class creation time. The Data Class decorator is called after the class is c…

The article doesn't discuss slots and default values. Here's the actual reference implementation for dataclasses where it's discussed: https://github.com/ericvsmith/dataclasses/issues/28

Your second solution works fine (and is what I'd referenced). For your first solution, I'd again redirect you to my statement "they don’t support __slots__ and default values" and the above link.

Re: Guide to Data Classes in Python 3.7

#9

Python keeps evolving -- great ! and also, consider support for a stable Python 2.7 LTS .. you know, for more than five years from now?

Python 2.7 has been stable and supported for almost 8 years -- how much more long term do you want? Even releases of Java only seem to get about 6 years of support.

Re: Guide to Data Classes in Python 3.7

#10

Python keeps evolving -- great ! and also, consider support for a stable Python 2.7 LTS .. you know, for more than five years from now?

I will answer you with a song:

    It's been a long road
    Getting from there to here
    It's been a long time
    But Py3 is finally here

    And I will see my dreams come alive at night
    I will touch the sky
    And Py2's not gonna hold me down no more
    No it's not gonna change my mind
In other words:

   Let it go, let it go...
Post reply on HN