News for hackers. Stuff that matters.
Re:Using attrs for everything in Python by nbadg on Thursday August 25, 2016 @02:54PM (#12361460)
Agreed.

From the post, it seems to me like another (IMO better) alternative to this awkward attr-specific convention and syntax etc would be to create a factory function for dynamic class definition, in a similar vein to namedtuples.

This is functionally similar to the metaclass route, and to be honest I'm not sure what the tradeoffs would be. Either way, I think metaprogramming is a much better approach than attrs if the problem you're trying to solve is "generic container classes take too long to get set up".

I think, were I to write something like this, I would probably do it using a metaclass, and then inject an __init__ on the MRO between the programmer-defined __init__ and the actual super().__init__. That way, you could call super().__init__(arg1, arg2, arg3) for the automatic attribute setting. You could also hook this in to some metaclass-defined storage containers to automate the __repr__ implementation, etc.

Re:Using attrs for everything in Python by StavrosK on Thursday August 25, 2016 @08:16PM (#12363457)
There is one:

>>> C2 = attr.make_class("C2", ["a", "b"])

>>> C2("foo", "bar")

C2(a='foo', b='bar')

Re:Using attrs for everything in Python by xapata on Thursday August 25, 2016 @10:17PM (#12363908)