Fwd: Proposal: Syntax for attribute initialisation in __init__ methods

Sam Ezeh sam.z.ezeh at gmail.com
Sat Apr 16 18:05:03 EDT 2022


> Perhaps I'm missing the point, but what functionality or advantage(s)
> does this give, over data-classes?

One advantage is maintaining control over the __init__ function
without having to write extra code to do so. In the linked discussion
from python-ideas, it was mentioned that keyword-only and
positional-only arguments can't be used with dataclasses [1].

> Maybe Dataclasses are not being used as much as one might hope, but they
> are relatively new, and many Python-Masters simply carry-on constructing
> classes the way they have for years...

I think one concern I have is that even if this is useful, it might
still fall to the same fate.

[1]: https://mail.python.org/archives/list/python-ideas@python.org/message/SCTXSEKOWDRDGVXXOEB7JUC6WE7XKGMO/


On Fri, 15 Apr 2022 at 22:30, dn <PythonList at danceswithmice.info> wrote:
>
> On 15/04/2022 23.19, Sam Ezeh wrote:
> ...
>
> Kudos for doing the research!
>
>
> > Some related implementations are attrs, dataclasses and the use of a
> > decorator. And there's potentially a point to be raised that the results
> > from the first query indicate that the @dataclasse decorator is not being
> > used enough. One advantage this proposal offers is control over the
> > arguments that the __init__ function takes.
> >
> > A downside to using a decorator is that it might become difficult to accept
> > arguments that don't need to be assigned to anything.
> >
> > I gave the example of the following code (unlike the above, this is not
> > taken from existing python source code). In this example, a decorator can't
> > assign all of the arguments to attributes or else it would produce code
> > that does something different.
> ...
>
>
> I will support anything which reduces 'boiler-plate' or 'make busy' work!
>
> Perhaps I'm missing the point, but what functionality or advantage(s)
> does this give, over data-classes?
>
> Maybe Dataclasses are not being used as much as one might hope, but they
> are relatively new, and many Python-Masters simply carry-on constructing
> classes the way they have for years...
>
> If data-classes give the impression of being 'syntactic sugar', there's
> no particular need to use them - and certainly no rule or demand.
>
> There are constructs where one might find it easier not to use them.
>
> @dataclass does allow init=False.
>
> There is an argument which says that all data-attributes should be
> 'created' inside an __init__() or __post_init__(), rather than
> 'somewhere', 'later'.
> --
> Regards,
> =dn
> --
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list