<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 22 April 2016 at 16:06, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Maybe I'm just misunderstanding how metaclasses should be written, but<br>
this seems like it ought to work. And it's not making any use of the<br>
AutoCreateDict - despite __prepare__ and __new__ clearly being called,<br>
the latter with an AutoCreateDict instance. But by the time it gets<br>
actually attached to the dictionary, we have a mappingproxy that<br>
ignores __missing__. The docs say "are translated to", implying that<br>
this will actually be equivalent.<br></blockquote><br></div><div class="gmail_quote">The mapping used during class body execution can be customised via __prepare__, but the resulting contents of that mapping are still copied to a regular dictionary when constructing the class object itself.<br><br></div><div class="gmail_quote">We deliberately don't provide a mechanism to customise the runtime dictionary used by object instances, regardless of whether they're normal instances or type definitions. In combination with the __dict__ descriptor only exposing a mapping proxy, this ensures that all Python level modifications to the contents go through the descriptor machinery - you can't get your hands on a mutable pointer to the post-creation namespace.<br><br></div><div class="gmail_quote">It looks like there *is* a missing detail in the data model docs in relation to this, though: <a href="https://docs.python.org/3/reference/datamodel.html#creating-the-class-object">https://docs.python.org/3/reference/datamodel.html#creating-the-class-object</a> should state explicitly that the namespace contents are copied to a plain dict (which is then never exposed directly to Python code), but it doesn't.<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">Cheers,<br></div><div class="gmail_quote">Nick.<br><br></div><div class="gmail_quote">P.S. I actually played around with an experimental interpreter build that dropped the copy-to-a-new-namespace step back when I was working on <a href="https://www.python.org/dev/peps/pep-0422/#new-ways-of-using-classes">https://www.python.org/dev/peps/pep-0422/#new-ways-of-using-classes</a>. It's astonishingly broken in the number of ways it offers to corrupt the interpreter state (since you can entirely bypass the descriptor machinery, which the rest of the interpreter expects to be impossible if you're not messing about with ctypes or C extensions), but kinda fun in the quirky action at a distance it makes possible :)<br></div><br>-- <br><div class="gmail_signature">Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</div>
</div></div>