<div dir="ltr"><div>On Thu, Jun 23, 2016 at 8:19 AM, 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra">It was a long time when I wrote this, but IIRC the breakage could express itself as a segfault or other C-level crash due to some internal state invariant of the type object being violated, not just an exception. The existence of ctypes notwithstanding, we take C-level crashes very seriously.</div></div></blockquote></div><div><br></div>Big digression: one can still obtain the dict if they really want to, even without using ctypes. I suppose don't actually mutate it unless you want to segfault.<div><br></div><div><div>>>> import gc</div><div>>>> class A(object): pass</div><div>>>> type(A.__dict__)</div><div><class 'mappingproxy'></div><div>>>> type(gc.get_referents(A.__dict__)[0])</div><div><class 'dict'></div><div>>>> gc.get_referents(A.__dict__)[0]['abc'] = 1</div><div>>>> A.abc</div><div>1</div><div>>>> </div></div><div><br></div><div>(One can also get it right from A, but A can have other references, so maybe that's less reliable.)</div><div><br></div><div>I think I wanted this at the time so I could better measure the sizes of objects. sys.getsizeof(A.__dict__) is very different from sys.getsizeof(gc.get_referents(A.__dict__)[0]), and also different from sys.getsizeof(A). For example:<br></div><div><br></div><div>>>> import gc</div><div><div>>>> class A(object): pass</div><div>>>> sys.getsizeof(A); sys.getsizeof(A.__dict__); sys.getsizeof(gc.get_referents(A.__dict__)[0])</div><div>976</div><div>48</div><div>288</div><div>>>> for i in range(10000): setattr(A, 'attr_%s' % i, i)</div><div>>>> sys.getsizeof(A); sys.getsizeof(A.__dict__); sys.getsizeof(gc.get_referents(A.__dict__)[0])</div><div>976</div><div>48</div><div>393312</div></div><div><br></div><div>(Fortunately, if you want to walk the object graph to measure memory usage per object type, you're probably going to be using gc.get_referents already anyway, so this is just confirmation that you're getting what you want in one corner case.)</div><div><br></div><div>-- Devin</div></div>