Skip to content

Using attrs

attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka dunder methods).

It's an excellent library. Personally, I use it heavily in all projects.

Inject classes

Here is an example of how to use it together with dependencies.

>>> from attr import attrs, attrib
>>> from dependencies import Injector

>>> @attrs
... class Order:
...     price = attrib()
...     items = attrib()

>>> class Container(Injector):
...     order = Order
...     price = (799, 99)
...     items = ["amplifier", "servo"]

>>> Container.order
Order(price=(799, 99), items=['amplifier', 'servo'])

As you can see, instances of a class defined with attrs are built completely the same way we build handwritten classes. But there is no need to write all this boilerplate by hand.

— ⭐ —