Earlier quoted context omitted.
I really, genuinely don't get the appeal. I don't follow the "less code = better" ideology so maybe that's a contributor but I really don't see how this: class Person: def __init__(self, name, age): self.name = name self.age = age is any worse than this: @dataclass class Person: name: str age: int I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost.
> I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost. Until you need them for debugging. And dataclasses make them free, at lesst syntactically.
Attrs – the Python library everyone needs (2016)
101–110 of 115 posts
Re: Attrs – the Python library everyone needs (2016)
#102Earlier quoted context omitted.
> I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost. Until you need them for debugging. And dataclasses make them free, at lesst syntactically.
I understand repr for debugging (though imo it's a deficiency of the language that custom objects don't have a repr which lists their attributes), but eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
It makes perfect sense that attributes be implementation details by default, and `@dataclass` is one of the ways to say they're not.
> eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
dataclass is intended for data holders, for which structural equality is an excellent default,
If you need a more bespoke business objects, then you probably should not use a dataclass.
Re: Attrs – the Python library everyone needs (2016)
#103Earlier quoted context omitted.
I understand repr for debugging (though imo it's a deficiency of the language that custom objects don't have a repr which lists their attributes), but eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
> though imo it's a deficiency of the language that custom objects don't have a repr which lists their attributes It makes perfect sense that attributes be implementation details by default, and `@dataclass` is one of the ways to say they're not. > eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or le…
So I wouldn't be so quick to abandon dataclasses in such a case.
Re: Attrs – the Python library everyone needs (2016)
#104Re: Attrs – the Python library everyone needs (2016)
#105Earlier quoted context omitted.
> though imo it's a deficiency of the language that custom objects don't have a repr which lists their attributes It makes perfect sense that attributes be implementation details by default, and `@dataclass` is one of the ways to say they're not. > eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or le…
dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required. So I wouldn't be so quick to abandon dataclasses in such a case.
That's completely besides the point, and lots of non-data objects should not be equatable at all, if they even can technically be.
Re: Attrs – the Python library everyone needs (2016)
#106Earlier quoted context omitted.
dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required. So I wouldn't be so quick to abandon dataclasses in such a case.
> dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required. That's completely besides the point, and lots of non-data objects should not be equatable at all, if they even can technically be.
Re: Attrs – the Python library everyone needs (2016)
#107Earlier quoted context omitted.
dataclasses have methods for iterating fields and inspecting types, so any feature can be added. There is also the benefit of type checking. My grief with dataclasses is how I can't inherit from dataclasses with default fields without making all child class fields also have defaults.
dataclasses author here. Does the keyword-only feature in 3.10 help you at all?
Re: Attrs – the Python library everyone needs (2016)
#108Earlier quoted context omitted.
> dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required. That's completely besides the point, and lots of non-data objects should not be equatable at all, if they even can technically be.
I don't understand your point. Are you saying that `__eq__` shouldn't be implemented at all? There's a switch for that, too.
This was in reply to this objection:
> eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
Re: Attrs – the Python library everyone needs (2016)
#109Earlier quoted context omitted.
Do you have concern with speed or memory footprint of pydantic compared to the rest (attrs, dataclasses etc)? Pydantic seems insistent on parsing/validating the types at runtime (which makes good sense for something like FastAPI).
We always used attrs with the runtime type validators anyway. Getting those types checked in Python was way more valuable to my teams than the minor boilerplate reduction. If you’re worried about the performance hit of extra crap happening at runtime… dear lord use another programming language. Dataclasses is just… meh. Pydantic and Attrs just have so many great features, I would never use dataclasses unless someone…
Python can be used quite successfully in high-performance environments if you are judicious about how you use it; set performance budgets, measure continuously, make sure to have vectorized interfaces, and have a tool on hand, like PyO3, Cython, or mypyc (you should probably NOT be using C these days, even if "rewrite in C" is the way this advice was phrased historically) ready to push very hot loops into something with higher performance when necessary. But if you redundantly validate everything's type on every invocation at runtime, it does eventually become untenable for anything but slow batch jobs if you have any significant volume of data.
Re: Attrs – the Python library everyone needs (2016)
#110Earlier quoted context omitted.
Yes. There needs to be a very good reason for me to pull in a third party library (in this day and age, given supply chain attacks, etc.). I don’t see what Attrs gives me that dataclasses does not.
Huh, it's the other way around for me. Just about every nontrivial Python project I write requires at least one third-party library, and one much more complicated than attrs at that, that I may as well use attrs too . All the complexity of having dependencies - both distributing them / setting up a virtualenv / etc., and whatever scrutiny I wish to do on dependencies - have a pretty big constant term from doing them…
Supply chain attacks are a complex and nuanced topic so there are plenty of reasons to be thoughtful about adopting new dependencies, but it's definitely not as simple as "just use the stdlib for everything".