API¶
attrs works by decorating a class using attr.s() and then optionally defining attributes on the class using attr.ib().
Note
When this documentation speaks about “attrs attributes” it means those attributes that are defined using attr.ib() in the class body.
What follows is the API explanation, if you’d like a more hands-on introduction, have a look at Examples.
Core¶
Helpers¶
attrs comes with a bunch of helper methods that make working with it easier:
attrs includes some handy helpers for filtering:
See Converting to Collections Types for examples.
Validators can be globally disabled if you want to run them only in development and tests but not in production because you fear their performance impact:
Validators¶
attrs comes with some common validators in the attrs.validators module:
Deprecated APIs¶
The serious business aliases used to be called attr.attributes and attr.attr. There are no plans to remove them but they shouldn’t be used in new code.