exception - python, __slots__, and "attribute is read-only" -


I want to create an object in Ajthan that has some characteristics and I mistakenly protect myself using the incorrect attribute name Want to The code is as follows:

  class MyClass (object): m = none # My specialty __slots__ = ("m") # Make sure there is no _m etc in the object = MyClass () # Create an am = "?" # This is a problem  

But after running this simple code, I get a very strange error:

  traceback (the most recent call final ): File "Test.py", line 8, & lt; Module & gt; A.m = "?" AttributeError: The 'test' object attribute 'M' is read only  

Is there a programmer who can save a little bit of time and let me know about "Read Only" errors Tells? If you declare an example variable using < __ slot __ , then Python creates one class as a variable in your case, with the same name in your case, this statement is class squared m < / Code> which you are defining in the following line:

  m = None # my attribute  

Here's what you need to do : Do not define a class variable called m , and m in __init __ method.

  class MyClass (object): __slots__ = ("m", def defy___it_ (self): self.m = none = MyClass () am = "?"  

As a side note, a comma is required after the elements with both elements, both work in your code because __ slot __ is a Accepts string or accepts one frequency / sequence of strings. Typically, (1,) or 1, and no (1) 1 element >.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -