fabiocaccamo / python-benedict

:blue_book: dict subclass with keylist/keypath support, built-in I/O operations (base64, csv, html, ini, json, pickle, plist, query-string, toml, xls, xml, yaml), s3 support and many utilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

fabiocaccamo opened this issue · comments

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))