Suppose I have a single class X
. The goal X
is to wrap list
or dict
and provide listening opportunities. Everything works well.
class X(object):
def __init__(self, obj)
self._obj = obj
def __getattr__(self, name):
def __getitem__(self, key):
return self._obj[key]
def __setitem__(self, key, val):
self._obj[key] = val
So, this can be used for wrapping dict
as follows:
x = X({
'foo' : False
})
x.listen('foo', callback)
X['foo'] = True
X.update({
'foo' : False
})
Or a list
:
x = X([1,2])
x.listen(callback)
X.append(1)
X[0] = 10
Great. Almost to what I wanted to achieve ...
Now the current problem is that since it is X
intended for list
and objects dict
, it cannot inherit from either. This means that I do not have magic class functions, for example __contains__
.
Which leads code like this
d = X({
'foo' : True
})
if 'foo' in d:
print 'yahoo!'
Throwing a KeyError
.
, X
. , , , self._obj
list
dict
.
, , , , , dict
list
.