Showing content from http://mail.python.org/pipermail/python-dev/attachments/20180217/a63625fd/attachment.html below:
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">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 class=""><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><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><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="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div>Â </div></font></span></div></div></div></blockquote><div><br></div># 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">self.name</a> = <a href="http://self.obj.name">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">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><div class="gmail_extra">Ben Lewis</div></div>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4