Replies: 4 comments
-
One of the advantages of attrs over namedtuple is default values. Now I tend to convert a namedtuple to a class whenever I want to add default values to avoid this: Message = namedtuple('Message', ['topic', 'payload', 'qos', 'retain'])
Message.__new__.__defaults__ = (0, False) |
Beta Was this translation helpful? Give feedback.
-
Looks amazing. I don't like namedtuple anyway :) |
Beta Was this translation helpful? Give feedback.
-
We're using attrs now! |
Beta Was this translation helpful? Give feedback.
-
Migrated this one as a test to see if closed issues are properly migrated (including they closing state). They are not. Please ignore this action/comment. |
Beta Was this translation helpful? Give feedback.
-
attrs is a Python package that allows you to write simple classes without any boilerplate. It's like a namedtuple, but better.
It makes it a lot easier to write small classes to pass data around. We use namedtuple 41 times currently in our codebase.
Normally I am not the person to propose adding dependencies to core, however I noticed that aiohttp started using attrs 2 weeks ago (aio-libs/aiohttp#2690) and thus it is already installed for a base install of Home Assistant.
Python 3.7 includes something similar to attrs called "Data Classes". However, it depends on PEP 526 which was only included in Python 3.6.
I think that using attrs can make our code easier to use. It also does not add any runtime costs since it does the work when the class is declared, not when a property is requested.
Beta Was this translation helpful? Give feedback.
All reactions