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:
class MyClass( object ) :
m = None # my attribute
__slots__ = ( "m" ) # ensure that object has no _m etc
a = MyClass() # create one
a.m = "?" # here is a PROBLEM
But after running this simple code, I get a very strange error:
Traceback (most recent call last):
File "test.py", line 8, in <module>
a.m = "?"
AttributeError: 'test' object attribute 'm' is read-only
Is there any wise programmer who can spare a bit of their time and enlighten me about “read-only” errors?
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 variablemthat you are defining at the following line:Here is what you need to do: Do not define a class variable called
m, and initialize the instance variablemin the__init__method.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 element1, use(1,)or1,and not(1).