I've been hearing buzz about the
attrs
Python module for a while ( for example ). I was recently writing a Python program where I had some structures and using
attrs
to define the classes involved would have made the code shorter and more obvious. At first I was all fired up to finally use
attrs
, but then I took a step back and reluctantly decided that doing so would be the wrong choice.
You see, this was code for work, and while my co-workers can work in Python, they're not Python people in the way that I am. They're certainly not up on the latest Python things and developments; to them, Python is a tool and they're happy to let it be if they don't need to immerse themselves in it. Naturally, they don't know anything about the
attrs
module.
If I used
attrs
, the code would be a bit shorter (and it'd be neat to actually use it), but my co-workers would have to learn at least something about
attrs
before they could understand my code to diagnose problems, make changes, or otherwise work on it. Using straightforward structure-style classes is boring, but it's not that much more code and it's code that's using a familiar, well established idiom that pretty much everyone is already familiar with.
Given this situation, I did the responsible thing and decided that my desire to play around with
attrs
was in no way a sufficient justification for inflicting another Python module to learn on my co-workers. Boring straightforward code has its advantages.
I can think of two things that would change this calculation. The first is if I needed more than just simple structure-style classes, so that
attrs
was saving me a significant chunk of code and making the code that remained much clearer. If I come out clearly ahead with
attrs
even after adding explanatory comments for my co-workers (or future me), then
attrs
is much more likely to be a win overall instead of just an indulgence.
(I think that the amount of usage and the size of the codebase matters too, but for us our codebases are small since we're just writing system utility programs and so on in Python.)
The second is if
attrs
usage becomes relatively widespread, so that my co-workers may well be encountering it in other people's Python code that we have to deal with, in online documentation, and so on. Then using
attrs
would add relatively little learning overhead and might even have become the normal idiom. This is part of why I feel much more free to use modules in the standard library than third-party modules; the former are, well, 'standard' in at least some sense.
(Mind you, these days I'm sufficiently out of touch with the Python world that I'm not sure how I'd find out if
attrs
was a big, common thing. Perhaps if Django started using and recommending it.)