<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 21 April 2016 at 12:51, Neil Girdhar <span dir="ltr"><<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@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"><div dir="ltr">Sometimes users inherit from builtin types only to find that their overridden methods are not called.  Instead of this being a trap for unsuspecting users, I suggest overriding the __new__ method of these types so that it will raise with an informative exception explaining that, e.g., instead of inheriting from dict, you should inherit from UserDict.<div><br></div><div>I suggest this modification to any Python implementation that has special versions of classes that cannot easily be extended, such as CPython.  If another Python implementation allows dict (e.g.) to be extended easily, then it doesn't have to raise.<br></div></div></blockquote><div><br></div></div>Builtins can be extended, you just have to override all the methods where you want to change the return type:<br><br>>>> from collections import defaultdict, Counter, OrderedDict<br>>>> issubclass(defaultdict, dict)<br>True<br>>>> issubclass(Counter, dict)<br>True<br>>>> issubclass(OrderedDict, dict)<br>True<br><br clear="all"></div><div class="gmail_extra">This isn't hard as such, it's just tedious, so it's often simpler to use the more subclass friendly variants that dynamically look up the type to return and hence let you get away with overriding a smaller subset of the methods.<br><br></div><div class="gmail_extra">Cheers,<br></div><div class="gmail_extra">Nick.<br><br></div><div class="gmail_extra">-- <br><div class="gmail_signature">Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</div>
</div></div>