class StrictDict(dict):

    def __setitem__(self, key, value):
        if key not in self:
            raise KeyError('{} is not a legal key of this StricDict'.format
                (repr(key)))
dict.__setitem__(self, key, value)
x = StrictDict({'puppies': 4, 'big_dogs': 2})
x['puppies'] = 23
x['dogs'] = 42
How to prevent key creation through d[key] = val