Let's for the sake of example assume I want to subclass dict
and have all keys capitalized:
class capdict(dict):
def __init__(self,*args,**kwds):
super().__init__(*args,**kwds)
mod = [(k.capitalize(),v) for k,v in super().items()]
super().clear()
super().update(mod)
def __getitem__(self,key):
return super().__getitem__(key.capitalize())
def __setitem__(self,key,value):
super().__setitem__(key.capitalize(),value)
def __delitem__(self,key):
super().__detitem__(key.capitalize())
This works to an extent,
>>> ex = capdict(map(reversed,enumerate("abc")))
>>> ex
{'A': 0, 'B': 1, 'C': 2}
>>> ex['a']
0
but, of course, only for methods I remembered to implement, for example
>>> 'a' in ex
False
is not the desired behavior.
Now, the lazy way of filling in all the methods that can be derived from the "core" ones
would be mixing in collections.abc.MutableMapping
. Only, it doesn't work here. I presume because the methods in question (__contains__
in the example) are already provided by dict
.
Is there a way of having my cake and eating it? Some magic to let MutableMapping
only see the methods I've overridden so that it reimplements the others based on those?