Note that there are some explanatory texts on larger screens.

plurals
  1. POSubclassing Python dictionary to override __setitem__
    primarykey
    data
    text
    <p>I am building a class which subclasses <code>dict</code>, and overrides <code>__setitem__</code>. I would like to be certain that my method will be called in all instances where dictionary items could possibly be set.</p> <p>I have discovered three situations where Python (in this case, 2.6.4) does not call my overridden <code>__setitem__</code> method when setting values, and instead calls <code>PyDict_SetItem</code> directly</p> <ol> <li>In the constructor</li> <li>In the <code>setdefault</code> method</li> <li>In the <code>update</code> method</li> </ol> <p>As a very simple test:</p> <pre><code>class MyDict(dict): def __setitem__(self, key, value): print "Here" super(MyDict, self).__setitem__(key, str(value).upper()) &gt;&gt;&gt; a = MyDict(abc=123) &gt;&gt;&gt; a['def'] = 234 Here &gt;&gt;&gt; a.update({'ghi': 345}) &gt;&gt;&gt; a.setdefault('jkl', 456) 456 &gt;&gt;&gt; print a {'jkl': 456, 'abc': 123, 'ghi': 345, 'def': '234'} </code></pre> <p>You can see that the overridden method is only called when setting the items explicitly. To get Python to always call my <code>__setitem__</code> method, I have had to reimplement those three methods, like this:</p> <pre><code>class MyUpdateDict(dict): def __init__(self, *args, **kwargs): self.update(*args, **kwargs) def __setitem__(self, key, value): print "Here" super(MyUpdateDict, self).__setitem__(key, value) def update(self, *args, **kwargs): if args: if len(args) &gt; 1: raise TypeError("update expected at most 1 arguments, got %d" % len(args)) other = dict(args[0]) for key in other: self[key] = other[key] for key in kwargs: self[key] = kwargs[key] def setdefault(self, key, value=None): if key not in self: self[key] = value return self[key] </code></pre> <p>Are there any other methods which I need to override, in order to know that Python will <em>always</em> call my <code>__setitem__</code> method?</p> <p><strong>UPDATE</strong></p> <p>Per gs's suggestion, I've tried subclassing UserDict (actually, IterableUserDict, since I want to iterate over the keys) like this:</p> <pre><code>from UserDict import *; class MyUserDict(IterableUserDict): def __init__(self, *args, **kwargs): UserDict.__init__(self,*args,**kwargs) def __setitem__(self, key, value): print "Here" UserDict.__setitem__(self,key, value) </code></pre> <p>This class seems to correctly call my <code>__setitem__</code> on <code>setdefault</code>, but it doesn't call it on <code>update</code>, or when initial data is provided to the constructor.</p> <p><strong>UPDATE 2</strong></p> <p>Peter Hansen's suggestion got me to look more carefully at dictobject.c, and I realised that the update method could be simplified a bit, since the built-in dictionary constructor simply calls the built-in update method anyway. It now looks like this:</p> <pre><code>def update(self, *args, **kwargs): if len(args) &gt; 1: raise TypeError("update expected at most 1 arguments, got %d" % len(args)) other = dict(*args, **kwargs) for key in other: self[key] = other[key] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload