7 ms·
Guide to Data Classes in Python 3.7
- hprotagonist 8y agoI 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.
- dfee 8y agoYou nailed it with "on any package that I write that has third party dependencies anyway". On my recent package I was going for zero dependencies (at py3.7+) so I used the backport of dataclasses. Ultimately, I threw out dataclasses too. But if I didn't need __repr__, __post_init__ and __slots__ with default values, I'd probably have just stuck with dataclasses :)
- lunchladydoris 8y agoI haven't watched it yet but I saw that Raymond Hettinger presented on data classes at PyCon last week [0]. [0]: https://www.youtube.com/watch?v=T-TwcmT6Rcw https://www.youtube.com/watch?v=T-TwcmT6Rcw
- neuland 8y agoI thought it was pretty good. He focused mostly on why you'd use this over typing.NamedTuple or hand-rolled code. Then, he talks about the couple limitations and where it'll go in the future.
- raymondh 8y agoHere are the slides for that presentation: https://twitter.com/raymondh/status/995693882812915712 https://twitter.com/raymondh/status/995693882812915712
- sologoub 8y agoData 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 https://stackoverflow.com/questions/472000/usage-of-slots
- dfee 8y agoThe 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.
- sologoub 8y agoOn the __slots__, the article claims that these do: https://realpython.com/python-data-classes/#optimizing-data-classes https://realpython.com/python-data-classes/#optimizing-data-... EDIT: From PEP 557: https://www.python.org/dev/peps/pep-0557/#support-for-automatically-setting-slots 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.”
- dfee 8y agoThe 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 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.
- sologoub 8y agoIt’s not my solution, just a direct quote from PEP.
- mistrial9 8y agoPython keeps evolving -- great ! and also, consider support for a stable Python 2.7 LTS .. you know, for more than five years from now?
- rspeer 8y agoPython 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.
- mistrial9 8y agoPerl 5 runs well after... twenty five years?
- deleted 8y ago[deleted]
- sametmax 8y agoPython 2 will not stop running in 2020. No bomb is gonna explode. The dozen of volunteers will just stop providing free work for bug fix and security update.
- rspeer 8y agoAnd Python 2 has run for 18, if we're counting by major versions. Heck, as long as you're okay with little changes like booleans not looking like integers anymore, you can run your Python 1 code 24 years later. But imagine if Python followed the example of Perl. Python 3 becomes a curious side project, everyone moves back to Python 2, which goes on for decades without new features. At last, Python can have all the popularity and relevance of... Perl 5?
- coldtea 8y ago>Python 2.7 has been stable and supported for almost 8 years -- how much more long term do you want? Forever? 2.x codebase are in the millions of lines of code in companies, and they won't be converted or go away anytime soon. And those will need security updates and bug fixes. Note that Google and Dropbox are still running tons of Python 2.x -- and those are two companies where Guido Vas Rossum himself has worked in the last 10 years. Not consider the average company with tons of Python 2.x code. It's not going anywhere soon. Heck, why is everyone surprised by this? Or is it just 20 year old first time pro devs that are surprised? The world still supports tons of Cobol and other "old" language code -- some running for 3-4 decades after a language went "out of fashion"...
- tedmiston 8y agoData Classes are a cool feature, useful, and a superior alternative to namedtuple. But while this was brewing, attrs ate their lunch. Data Classes just don't go far enough compared to what attrs [1] has provided for a while now. If there's a compelling reason to switch, I haven't found it yet. Can I use Data Classes in a library supporting Python 2.7, 3.4-3.7? I don't think the answer is yes today. [1]: https://github.com/python-attrs/attrs https://github.com/python-attrs/attrs
- neuland 8y agoThe standard library is not going to have more advanced features than what the community can do, if only because of the release cycle. Meanwhile, I was not a user of attrs before, rather I used collections.namedtuple and then typing.NamedTuple. So, data classes is a win for me and other people like me who weren't using attrs. And you can continue to use attrs without being bothered at all. In Raymond Hettinger's talk [0] at PyCon 2018, he even says that more features from attrs will likely get added, such as data validation [1]. And that his talk was the one chance for the developer (Eric Smith) to get praise, because everyone is going to instantly second guess every decision he made. [0] https://www.youtube.com/watch?v=T-TwcmT6Rcw https://www.youtube.com/watch?v=T-TwcmT6Rcw [1] see 42:36 on future directions
- Rotareti 8y ago> Data Classes are a cool feature, useful, and a superior alternative to namedtuple. I'm curious, how is it "superior" to NamedTuples?
- bb88 8y agoThe PEP lists the rationale and some discussion. https://www.python.org/dev/peps/pep-0557/#rationale https://www.python.org/dev/peps/pep-0557/#rationale https://www.python.org/dev/peps/pep-0557/#id10 https://www.python.org/dev/peps/pep-0557/#id10
- jwilk 8y agoAnother alternative to data classes is types.SimpleNamespace, available since Python 3.3: https://docs.python.org/3/library/types.html#types.SimpleNamespace https://docs.python.org/3/library/types.html#types.SimpleNam...
- talltimtom 8y agoThis marks the beginning of the end for python as a dynamic language.