Why is a class __dict__ a mappingproxy?

后端 未结 3 1554
长情又很酷
长情又很酷 2020-12-02 18:21

I wonder why a class __dict__ is a mappingproxy, but an instance __dict__ is just a plain dict

>>&g         


        
3条回答
  •  半阙折子戏
    2020-12-02 18:35

    A mappingproxy is simply a dict with no __setattr__ method.

    You can check out and refer to this code.

    from types import MappingProxyType
    d={'key': "value"}
    m = MappingProxyType(d)
    print(type(m)) # 
    
    m['key']='new' #TypeError: 'mappingproxy' object does not support item assignment
    

    mappingproxy is since Python 3.3. The following code shows dict types:

    class C:pass
    ci=C()
    print(type(C.__dict__)) #
    print(type(ci.__dict__)) #
    

提交回复
热议问题