kwargs overriding dict subclass
<p>A minimal example is as follows:</p>

<pre><code>class sdict(dict):
    class dval:
        def __init__(self, val):
            self.val = val

    def __init__(self, args):
        dictionary = dict(args)
        for k,v in dictionary.items():
            self[k] = v

    def __setitem__(self,key,value):
         try:                   self[key].val = value
         except KeyError:       dict.__setitem__(self,key,self.dval(value))
    def __getitem__(self,key):
         return dict.__getitem__(self,key).val
</code></pre>

<p>Now I can use this class inside a container to override assignment stuff:</p>

<pre><code>class cont(object):
    def __init___(self):
        self._mydict = sdict(())
    @property
    def mydict(self):
        return self._mydict
    @mydict.setter
    def mydict(self,dict_initializor):
        self._mydict = sdict(dict_initializor)
        return self.mydict
</code></pre>

<p>I use this so my container holds a dictionary with pointable primitives (i.e. mutable in python language) but whenever I retrieve the item I get the primitive itself. In my case floats, which I multiply, add etc... Adding __str__/__repr__ functions makes this work when printing these, but for debugging purposes these are omitted.</p>

<p>This works nicely almost everywhere, except when passing this along using kwargs:</p>

<pre><code>def test(a,b): return a*b
c = cont()
c.mydict = {'a':1,'b':2}
print c.mydict['a']+c.mydict['b'],type(c.mydict['a']+c.mydict['b'])
print test(**c.mydict)
</code></pre>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>3 &lt;type 'int'&gt;
Traceback (most recent call last):
  File "D:\kabanus\workspace\py-modules\test.py", line 32, in &lt;module&gt;
    print test(**c.mydict)
  File "D:\kabanus\workspace\py-modules\test.py", line 28, in test
    def test(a,b): return a*b
TypeError: unsupported operand type(s) for *: 'instance' and 'instance'
</code></pre>

<p>Obviously the unpacking procedure does not use __getitem__ to create values for the keywords. I also attempted to override values() and items() with no success. Next I set all dict methods not defined by me to None to see if any are called - no success either.</p>

<p>Bottom line - how do you get **kwargs unpacking to use __getitem__ or some other method?<br>
Is this even possible?</p>

<p>I also want to avoid defining mul, add, div and so forth if possible in class dval.</p>

<p>Found the answer here, and there should be a way to make this easier to find:
<a href="http://stackoverflow.com/questions/19526300/does-argument-unpacking-use-iteration-or-item-getting#19526400">Does argument unpacking use iteration or item-getting?</a></p>

<p>Summary:
Inheriting from builtins (dict,list...) is problematic since python may ignore your python API entirely (as it did for me) and use the underlying C. This happens in argument unpacking.</p>

<p>Solution: use the available abstractions. In my example add:</p>

<pre><code>from UserDict import UserDict
</code></pre>

<p>and replace all "dict" occurrences in the code with UserDict. This solution should be true for lists and tuples. </p>

python dictionary kwargs