This is only remotely relevant but I recently learned that the related `dataclasses` is implemented by constructing _string representations_ of functions and calling `exec` on them. https://github.com/python/cpython/blob/3.10/Lib/dataclasses.... Kind of blew my mind
Attrs – the Python library everyone needs (2016)
91–100 of 115 posts
Re: Attrs – the Python library everyone needs (2016)
#92Earlier 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)
#93I've never understood the appeal of these "define struct-like-object" libraries (in any language; I've never understood using the standard library's "Struct" in Ruby). My preferred solution for addressing complexity is also to decompose the codebase into small, understandable, single-purpose objects, but so few of them end up being simple value objects like Point3D. Total ordering and value equality make sense for va…
Ignore all of the validation aspects. In python, you have tuples, (x, y, z), then you have namedtuples and then attrs/dataclasses/pydantic-style shorthand classes. These are useful even if only due to the "I can take the three related pieces of information I have and stick them next to each other". That is, if I have some object I'm modelling and it has more than a single attribute (a user with a name and age, or an…
Re: Attrs – the Python library everyone needs (2016)
#94Earlier quoted context omitted.
Not a python guy, so confused as to why a thing called namedtuple behaves like dataclasses, what are their different usecases?
From a users perspective data classes look kind of like a C struct and in particular include type annotations so fit well with type checkers. They also allow for default values and give more control over generating equality, hash, string and initialisation methods. Comparatively named tuples are an older language feature which essentially allow you to define named accessors for tuple elements. IIRC, these days you ca…
You can even type dictionaries this way: https://docs.python.org/3/library/typing.html#typing.TypedDi...
Re: Attrs – the Python library everyone needs (2016)
#95Re: Attrs – the Python library everyone needs (2016)
#96Re: Attrs – the Python library everyone needs (2016)
#97Earlier quoted context omitted.
Welp not sure why I caught downvotes for using the two most common static analysis tools for Python, but here we are on Hacker News
Likely the reason is that you really only need just mypy. It handles the type issues. Pylint is useful, but doesn't really overlap with mypy in terms of what it catches, and there's no need to use pyright or to write type-verifying unit tests, or to do runtime type validation if you have mypy. Using mypy gives you the type safety equivalent of a compiled language. If you're using mypy, you don't need any additional v…
Yeah mypy will get you maybe 90% of the way there, but the swiss cheese approach of stacking a few tools, even though there are redundancies, helps plug almost all of the holes.
People can argue about unit tests all day, but there’s very little cost to stacking multiple static analysis tools.
Re: Attrs – the Python library everyone needs (2016)
#98Earlier quoted context omitted.
From a users perspective data classes look kind of like a C struct and in particular include type annotations so fit well with type checkers. They also allow for default values and give more control over generating equality, hash, string and initialisation methods. Comparatively named tuples are an older language feature which essentially allow you to define named accessors for tuple elements. IIRC, these days you ca…
FWIW, you can also add type annotations for namedtuples, with a syntax similar to data classes: https://docs.python.org/3/library/typing.html#typing.NamedTu... You can even type dictionaries this way: https://docs.python.org/3/library/typing.html#typing.TypedDi...
Re: Attrs – the Python library everyone needs (2016)
#99Earlier 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…
As I think I made clear in PEP 557, and every time I discuss this with anyone, dataclasses owes a lot to attrs. I think attrs made some great design decisions, in particular to metaclasses or base classes.
Re: Attrs – the Python library everyone needs (2016)
#100Earlier quoted context omitted.
Ignore all of the validation aspects. In python, you have tuples, (x, y, z), then you have namedtuples and then attrs/dataclasses/pydantic-style shorthand classes. These are useful even if only due to the "I can take the three related pieces of information I have and stick them next to each other". That is, if I have some object I'm modelling and it has more than a single attribute (a user with a name and age, or an…
Note that that "location" property should be a method instead of property to signal that it does something potentially complex and slow. Making it a property practically guarantees that someone will use it in a loop without much second thought, and that's how you get N+1.