Author Kirk Hansen
Recipients Kirk Hansen, docs@python
Date 2016-07-18.13:30:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1468848651.49.0.804648096873.issue27561@psf.upfronthosting.co.za>
In-reply-to
Content
I tried subclassing dict, and overriding its __setitem__ and __getitem__ and got some interesting results. See http://stackoverflow.com/questions/38362420/subclassing-dict-dict-update-returns-incorrrect-value-python-bug?noredirect=1#comment64142710_38362420 for an example.

I tried sub-classing UserDict.UserDict and experienced some of the same problems. Eventually, I discovered that subclassing MutableMapping was my best bet.

After an hour or two of searching and reading I discovered that CPython will not call overridden built-in methods of the same object (https://pypy.readthedocs.io/en/latest/cpython_differences.html#subclasses-of-built-in-types). This behaviour could (and did) cause some hard to track down bugs in code. 

I briefly looked at the later versions of python documentation and didn't see a mention of this (sorry if it's there), but python2.7 definitely does not mention this. In fact, python2.7 seems to __encourage__ users to subclass the built-ins (see the note https://docs.python.org/2.7/library/stdtypes.html?highlight=dict#built-in-types). Subclassing dict to __extend__ functionality is great, but there should be a big bad warning when trying to __override__ built-ins like __setitem__ and __getitem__.
History
Date User Action Args
2016-07-18 13:30:51Kirk Hansensetrecipients: + Kirk Hansen, docs@python
2016-07-18 13:30:51Kirk Hansensetmessageid: <1468848651.49.0.804648096873.issue27561@psf.upfronthosting.co.za>
2016-07-18 13:30:51Kirk Hansenlinkissue27561 messages
2016-07-18 13:30:50Kirk Hansencreate