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:
<
You are completely misusing __slots__
. It prevents the creation of __dict__
for the instances. This only makes sense if you run into memory problems with many small objects, because getting rid of __dict__
can reduce the footprint. This is a hardcore optimization that is not needed in 99.9% of all cases.
If you need the kind of safety you described then Python really is the wrong language. Better use something strict like Java (instead of trying to write Java in Python).
If you couldn't figure out yourself why the class attributes caused these problems in your code then maybe you should think twice about introducing language hacks like this. It would probably be wiser to become more familiar with the language first.
Just for completeness, here is the documentation link for slots.