Guide to Data Classes in Python 3.7
realpython.com
Guide to Data Classes in Python 3.7
1–10 of 48 posts
Re: Guide to Data Classes in Python 3.7
#2That 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
#3Re: Guide to Data Classes in Python 3.7
#4Additional reading on __slots__: https://stackoverflow.com/questions/472000/usage-of-slots
Re: Guide to Data Classes in Python 3.7
#51) 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
#6The 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.
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
#7The 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…
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
#8Re: Guide to Data Classes in Python 3.7
#9Python keeps evolving -- great ! and also, consider support for a stable Python 2.7 LTS .. you know, for more than five years from now?
Re: Guide to Data Classes in Python 3.7
#10Python keeps evolving -- great ! and also, consider support for a stable Python 2.7 LTS .. you know, for more than five years from now?
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...