A python class that acts like dict

前端 未结 9 2069

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:35

    Don’t inherit from Python built-in dict, ever! for example update method woldn't use __setitem__, they do a lot for optimization. Use UserDict.

    from collections import UserDict
    
    class MyDict(UserDict):
        def __delitem__(self, key):
            pass
        def __setitem__(self, key, value):
            pass
    

提交回复
热议问题