class ImmutableDict(object): def __init__(self, **kwargs): self.kwargs = kwargs def __getitem__(self,key): try: return self.kwargs[key] except: raise TypeError("'ImmutableDict' objects are immutable") def pop(self,key): raise TypeError("'ImmutableDict' objects are immutable") def get(self,key): return self.kwargs.get(key)
def __setitem__(self,key):
raise TypeError("'ImmutableDict' objects are immutable")
初始化方法中为什么会用到**kwargs,可参考这篇文章https://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html