python, __slots__, and “attribute is read-only”

后端 未结 5 1236
無奈伤痛
無奈伤痛 2021-02-02 11:59

I want to create an object in python that has a few attributes and I want to protect myself from accidentally using the wrong attribute name. The code is as follows:

<         


        
5条回答
  •  旧时难觅i
    2021-02-02 12:17

    class MyClass( object ) :
        m = None # my attribute
    

    The m here is the class attributes, rather than the instance attribute. You need to connect it with your instance by self in __init__.

提交回复
热议问题