Ronan's Tech Blog

Feb 16, 2019

Attrs library for Python

A quick advertisement.

I love Hynek Schlawack's attrs library. It's had a subtle but greatly clarifying influence on my code since I discovered it.

If you're not familiar with that library then I highly recommend reading about it.

It means you can write simple objects like

@attr.s
class Thing(object):
    value = attr.ib()

Which are equivalent to

class Thing(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        # ... The usual thing

    def __eq__(self):
        # ... The usual thing

    # ... All the other dunder methods

The docs have much better examples than I could repeat here. You should check it out.