class DictConditional(dict):

    def __init__(self, cond=lambda x: x is not None):
        self.cond = cond

def __setitem__(self, key, value):
    if key in self or self.cond(value):
        dict.__setitem__(self, key, value)
d = DictConditional(lambda x: x != 0)
d['foo'] = 0
assert 'foo' not in d
Only add to a dict if a condition is met