Proposal: Syntax for attribute initialisation in __init__ methods

dn PythonList at DancesWithMice.info
Fri Apr 15 17:26:43 EDT 2022


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


More information about the Python-list mailing list