[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)

Eric V. Smith eric at trueblade.com
Wed May 17 10:16:04 EDT 2017


On 5/17/17 2:11 AM, Eric V. Smith wrote:
> One use case that attrs satisfies (as does namedtuple) that I'd like to
> make sure we allow for in any solution is dynamically creating classes
> where you don't know the field names until runtime. I run in to this
> when reading anything with columnar metadata, like databases or CSV
> files. The attr.s "these" parameter solves this for me:
>
> fields = ['a', 'b', 'c']
>
> @attr.s(these={f:attr.ib() for f in fields})
> class Foo:
>     pass

I should also have mentioned attr.make_class.



More information about the Python-ideas mailing list