<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jun 22, 2016 at 7:17 AM, Random832 <span dir="ltr"><<a href="mailto:random832@fastmail.com" target="_blank">random832@fastmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The documentation states: """Objects such as modules and instances have<br>
an updateable __dict__ attribute; however, other objects may have write<br>
restrictions on their __dict__ attributes (for example, classes use a<br>
dictproxy to prevent direct dictionary updates)."""<br>
<br>
However, it's not clear from that *why* direct dictionary updates are<br>
undesirable. This not only prevents you from getting a reference to the<br>
real class dict (which is the apparent goal), but is also the<br>
fundamental reason why you can't use a metaclass to put, say, an<br>
OrderedDict in its place - because the type constructor has to copy the<br>
dict that was used in class preparation into a new dict rather than<br>
using the one that was actually returned by __prepare__.<br>
<br>
[Also, the name of the type used for this is mappingproxy, not<br>
dictproxy]<br></blockquote></div><br></div><div class="gmail_extra">This is done in order to force all mutations of the class dict to go through attribute assignments on the class. The latter takes care of updating the class struct, e.g. if you were to add an `__add__` method dynamically it would update tp_as_number->nb_add. If you could modify the dict object directly it would be more difficult to arrange for this side effect.<br clear="all"></div><div class="gmail_extra"><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></div>