kabanus
kabanus

Reputation: 25895

kwargs overriding dict subclass

A minimal example is as follows:

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

Now I can use this class inside a container to override assignment stuff:

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

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.

This works nicely almost everywhere, except when passing this along using kwargs:

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)

Output:

3 <type 'int'>
Traceback (most recent call last):
  File "D:\kabanus\workspace\py-modules\test.py", line 32, in <module>
    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'

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.

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

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

Upvotes: 2

Views: 466

Answers (1)

kabanus
kabanus

Reputation: 25895

Found the answer here, and there should be a way to make this easier to find: Does argument unpacking use iteration or item-getting?

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.

Solution: use the available abstractions. In my example add:

from UserDict import UserDict

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

Upvotes: 1

Related Questions