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

后端 未结 5 1240
無奈伤痛
無奈伤痛 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条回答
  •  庸人自扰
    2021-02-02 12:10

    When you declare instance variables using __slots__, Python creates a descriptor object as a class variable with the same name. In your case, this descriptor is overwritten by the class variable m that you are defining at the following line:

      m = None # my attribute
    

    Here is what you need to do: Do not define a class variable called m, and initialize the instance variable m in the __init__ method.

    class MyClass(object):
      __slots__ = ("m",)
      def __init__(self):
        self.m = None
    
    a = MyClass()
    a.m = "?"
    

    As a side note, tuples with single elements need a comma after the element. Both work in your code because __slots__ accepts a single string or an iterable/sequence of strings. In general, to define a tuple containing the element 1, use (1,) or 1, and not (1).

提交回复
热议问题