A python class that acts like dict

前端 未结 9 2067

I want to write a custom class that behaves like dict - so, I am inheriting from dict.

My question, though, is: Do I need to create a priva

9条回答
  •  温柔的废话
    2020-11-30 18:19

    Here is an alternative solution:

    class AttrDict(dict):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.__dict__ = self
    
    a = AttrDict()
    a.a = 1
    a.b = 2
    

提交回复
热议问题