python-benedict icon indicating copy to clipboard operation
python-benedict copied to clipboard

Add `freeze` method (to make the dict immutable).

Open fabiocaccamo opened this issue 3 years ago • 1 comments

fabiocaccamo avatar Jan 05 '22 15:01 fabiocaccamo

Hello, is it possible to add an attribute to a benedict? I wanted to add an is_frozen attribute, but I encountered an error like this:

    raise AttributeError(attr_message) from None
AttributeError: 'benedict' object has no attribute 'is_frozen'

I've written:

class benedict(KeyattrDict, KeypathDict, IODict, ParseDict):
    def __init__(self, *args, **kwargs):
        ....
        self.is_frozen = False

    def freeze(self):
        self.is_frozen = True
        
    def __setitem__(self, key, value):
        if self.is_frozen:
            raise TypeError("This dictionary is immutable")
        return super().__setitem__(key, self._cast(value))

thesayfulla avatar Jul 05 '24 08:07 thesayfulla