yifeiyin / BACKPACK

Yifei's blog, coding notes, environment setup and settings, etc.
0 stars 0 forks source link

JavaScript's Object-Dictionary Behaviour in Python #23

Open yifeiyin opened 5 years ago

yifeiyin commented 5 years ago

https://goodcode.io/articles/python-dict-object/

class objdict(dict):
    def __getattr__(self, name):
        if name in self:
            return self[name]
        else:
            raise AttributeError("No such attribute: " + name)

    def __setattr__(self, name, value):
        self[name] = value

    def __delattr__(self, name):
        if name in self:
            del self[name]
        else:
            raise AttributeError("No such attribute: " + name)