I have a library function that uses **kw, but I want to pass a dictionary-like class so that I can override __getitem__to track its access 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)
source
share