5 ms·
dataclasses are the best recent addition to Python, IMO, maybe tied with ordered dicts. Python definitely doesn't have conceptual simplicity, because there are
by kzrdude 6y ago
dataclasses are the best recent addition to Python, IMO, maybe tied with ordered dicts.
Python definitely doesn't have conceptual simplicity, because there are many competing concepts in the language, and lack of focus on uniformity. (Example - enums are implemented with a metaclass and dataclasses with a class decorator - arguably the latter is the wrong choice, because it is already evident it limits what kind of features can be added to dataclasses).
Another example just because it makes me irate - `typing.NamedTuple` has been added as an "alternative" to `collections.namedtuple`. Same name, new capitalization, slightly different features. Add to that, a collection type that's inside the module `typing` which is supposed to be for.. type annotations, not actual implementations.
- crazypython 6y agoNo, (optional) static typing is the best recent addition to Python. It allows IDEs to catch a large variety of errors. The static type system is part of the language and has multiple implementations. It's Hindley-Milner, making it two-way, like Haskell's type system.
- kzrdude 6y agoThere is no "No", you can just add your opinion about your favourite feature instead.
- sn9 6y agoWait what? Are you saying Python has a Hindley-Milner type system? What's it called? Google didn't turn up anything.
- kzrdude 6y agoPython doesn't have it but I think they are talking about mypy, options typechecker tool that has type inference.
- crazypython 6y agoIt's called PEP-484, and it's part of the language https://www.python.org/dev/peps/pep-0484/ https://www.python.org/dev/peps/pep-0484/ Pyre and PyCharm are third-party implementations of PEP-484.
- ericvsmith 6y agoAuthor of dataclasses here. > (Example - enums are implemented with a metaclass and dataclasses with a class decorator - arguably the latter is the wrong choice, because it is already evident it limits what kind of features can be added to dataclasses). I'm curious about what features can't be added to dataclasses because it's not using a metaclass. If anything, I think that not using a metaclasses is a good thing: it allows you to use @dataclass with classes that do require a metaclass. That metaclass would likely be incompatible with the metaclass that such a hypothetical @dataclass implementation would use.
- mcovalt 6y agoThanks for not using metaclasses for dataclasses. I’m looking forward to all libraries replacing metaclasses. For instance, in Django, I think all their metaclasses can be replaced by __init__subclass__. The main issue I have with them is how difficult it can be to compose a new abstract class using upstream classes with them.
- noitpmeder 6y agoI love dataclasses, incredibly simple to use but so useful. My only gripe is no first-class support for "__slots__".
- ericvsmith 6y agoI have a decorator to add support for slots: https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py https://github.com/ericvsmith/dataclasses/blob/master/datacl... (although I'll admit I haven't tested it in a while). The reason it's not an option by default is because it would be the one case where the decorator would have to return a new class, and I didn't want to do that on the first version. As it is, @dataclass just modifies an existing class. I might bite the bullet add a slots option, and we've had discussions on adding a language feature to automatically add slots to a class, based on annotations and maybe some other magic. If we did that, we wouldn't need to return a new class. But it's not a front-burner task for me.
- 6y ago