[Python-ideas] Python 3.7 dataclasses attribute order

Eric V. Smith eric at trueblade.com
Wed Oct 24 05:43:44 EDT 2018


On 10/24/2018 5:30 AM, Anders Hovmöller wrote:
> Well that seems super unfortunate. You can opt out of the auto generate 
> constructor and do it yourself:
> 
>    @dataclass(init=False)
>    class Foo:
>        foo: str
>        bar: str = None
>        baz: str
> 
>        def __init__(self, *, foo, bar = None, baz):
>            self.foo = foo
>            self.bar = bar
>            self.baz = baz
> 
> 
>    Foo(foo='a', bar='b', baz='c')
> 
> but this seems to take away from the utility of dataclasses. One could 
> imagine there being a new argument to @dataclass that would make this 
> work. Something like:
> 
> 
> @dataclass(init_kwargs_only=True)
>    class Foo:
>        foo: str
>        bar: str = None
>        baz: str
> 
> where you would then get an auto generated constructor like with keyword 
> only arguments. Personally I think this should have been the default, 
> but it's at least a nice addition now.

https://bugs.python.org/issue33129

I definitely wouldn't want this to be the default. And as you say, it's 
too late anyway.

I haven't decided how the interaction of per-field and whole class 
versions of keyword-only should operate.

Eric



More information about the Python-ideas mailing list