Otherwise, maybe it could be possible to rewrite assignment done in
__post_init__ . However, I'm not sure that it will be generic enough.
Arthur Pastel,



On Thu, Dec 12, 2019 at 1:10 AM Andrew Barnert <[email protected]> wrote:

> On Dec 11, 2019, at 15:40, Arthur Pastel <[email protected]> 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 ...
>
> If you don’t understand freezable, I guess you’re not going to propose it
> as an answer, so don’t worry about it?
>
> >> 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 guess, but not a good one. Stack inspection is generally considered a
> huge code smell. For example, whenever someone on this list says “Python
> should add X”, and someone replies “you can already do X if you’re willing
> to do some hacky stack inspection”, nobody takes that as an argument that
> Python doesn’t need to add X.
>
> Also, stack inspection isn’t even guaranteed to work in other Python
> implementations, so someone would have to come up with their own different
> way of implementing this feature for such implementations anyway.
>
> >> Add an extra hidden attribute to every instance just
> >> to track whether you’re inside __post_init__ so __setattr__ can check
> it?
> > This is better in my opinion.
>
> Better, but still not good. People care how big their objects are,
> especially when they’re using slots or namedtuple or dataclass. If most of
> my program’s memory use is zillions of instances of tiny data classes, and
> Python 3.10 makes every one of them 8 bytes bigger so my total memory use
> goes up 32%, I’m not going to be happy.
>
> All of my implementations are terrible ideas; arguing for one of them is
> unlikely to get you very far. You need to come up with something that isn’t
> terrible instead.
>
> > 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.
>
> __setattr__, like most special methods, has to appear on the class, not
> the instance. So you can’t dynamically redefine it. (Plus, even if you
> could, it would again add 8 bytes to every instance.)
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/7XHQQZPFXTZXCMOGH4O64O3HAL4PMQXV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to