<div dir="ltr"><div>Why can'y you make `name` on `NamedObjectItem` a property that returns `<a href="http://self.obj.name">self.obj.name</a>`? Why store a duplicate copy of the name?<br><br></div>PS. I have to ponder why frozen dataclasses don't use `__new__`.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 16, 2018 at 11:43 PM, Ben Lewis <span dir="ltr"><<a href="mailto:benlewisj@gmail.com" target="_blank">benlewisj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Sat, Feb 17, 2018 at 6:40 PM, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><div><br></div></span><div>That's a pretty tricky proposal, and one that's been debated on and off for a long time in other contexts. And that flag would somehow have to be part of every instance's state.</div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>In general the right way to initialize an immutable/frozen object is not through __init__ but through __new__ -- have you tried that?<br></div></div></div></div></blockquote><div><br></div></span><div>Constructing it throught __new__ doesn't actually work as it has no way to alter the arguments that are passed into __init__, I think creating a metaclass that overides __call__ is required to acheive the desired result. Although a factory classmethod would acheive similar api.</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>Also, a small example that demonstrates your need would do wonders to help us understand your use case better.<span class="m_-7977929340697853024HOEnZb"><font color="#888888"><br></font></span></div><span class="m_-7977929340697853024HOEnZb"><font color="#888888"><div> </div></font></span></div></div></div></blockquote><div><br></div></span># unrelated object<br>class NamedObject:<br> @property<br> def name(self) -> str:<br> return "some name"<br><br>// has may subclasses<br>@dataclass<br>class Item:<br> name: str<br><br><br>@dataclass<br>class NamedObjectItem(Item):<br> name: str = field(init=False)<br> obj: NamedObject<br><br> def __post_init__(self):<br> <a href="http://self.name" target="_blank">self.name</a> = <a href="http://self.obj.name" target="_blank">self.obj.name</a></div><div class="gmail_quote"><br></div><div class="gmail_quote">This works fine, until I decided them Item and therefore all subclasses should be frozen as no instances are mutated</div><div class="gmail_quote">and if they are ever in the future then its a bug. But to do this the following factory method needs to be added:</div><div class="gmail_quote"><br></div>@classmethod<br>def create(cls, obj: NamedObject, *args, **kwargs):<br> return cls(<a href="http://obj.name" target="_blank">obj.name</a>, obj, *args, **kwargs)<br><br>This doesn't look that bad but all fields(up to the last field used that would have been used in __post_init__) needs to be declared in the signature.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Thanks</div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra">Ben Lewis</div></font></span></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>