def __setitem__(self, name, value):
    """
        a dictionary can have any value for name, any value for value
        a list will have an integer for name, any value for value
        """
valid_key = self.valid_key
valid_value = self.valid_value
if valid_key is not None:
    if not valid_key(name):
        raise Exception('%r not allowed as key/index' % type(name))
if valid_value is not None:
    if not valid_value(value):
        raise Exception('%r not allowed as value' % value)
self.obj[name] = value
Capturing the external modification of a mutable python object serving as an instance class variable