Apr 182025
 

The History
Before dataclasses were added to Python in version 3.7 — in June of 2018 — the
__init__ special method had an important use. If you had a class
representing a data structure — for example a 2DCoordinate, with x and y
attributes — you would want to be able to construct it as 2DCoordinate(x=1,
y=2), which would require you to add an __init__ method with x and y
parameters.
The other options available at the time all had pretty bad problems:

You could remove 2DCoordinate from your public API and instead expose a make_2d_coordinate function and make it non-importable, but then how would you document your return or parameter types?
You could document the x and y attributes and make the user assign…

External feed Read More at the Source: https://blog.glyph.im/2025/04/stop-writing-init-methods.html

 2025-04-18  Add comments

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)