On 12/11/2019 6:36 PM, Arthur Pastel wrote:
If dataclass handled freezeable types (the objects are mutable until you call
freeze,
after which they’re not), this would be easy (frozen now just means freezable,
plus freeze
is called by the generated __init__ right after the __post_init__). But it
doesn’t,
because freezing is complicated.
I don't really get your point here ...
So, what else could you do? Make __setattr__ check the stack and see if it’s
being
called from type(self).__post_init__?
Yes I think this is one possibility.
I don't think all implementations of Python would handle this stack
inspection very well. Especially because it might need to look back
multiple frames. And I could see some complications with frozen
__post_init__'s creating other instances of the same (frozen) type, but
I haven't thought it through.
Add an extra hidden attribute to every instance just
to track whether you’re inside __post_init__ so __setattr__ can check it?
I don't want to add attributes that weren't defined in the class definition.
This is better in my opinion.
On top of this, to avoid having the extra hidden attribute maybe it would be
possible to dynamically define the __setattr__ method in the end of the
__post_init__ call.
But __setattr__ (like all special methods) is defined on the class, not
on an instance. And there may be no generated __init__ to do the work,
anyway.
Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/5BTGPKOMA5D4QEKOOGHFNOLTO2ZNSZ37/
Code of Conduct: http://python.org/psf/codeofconduct/