I have a library function that makes use of **kw, but I want to pass a dictionary-like class so that I can override __getitem__ to track its accesses to data in the dictionary. For example, in the code below calling libfn does not print Accessed but libfn2 does.
class Dtracker(dict):
def __init__(self):
dict.__init__(self)
def __getitem__(self,item):
print "Accessed %s" % str(item)
return dict.__getitem__(self, item)
def libfn(**kw):
a = kw["foo"]
print "a is %s" % a
return a
def libfn2(kw):
a = kw["foo"]
print "a is %s" % a
return a
d = Dtracker()
d["foo"] = "bar"
libfn(**d)
libfn2(d)
Do this
This more-or-less works