Sometimes I need to change the name of a Python class attribute that is being already extensively used in the rest of the code.
The problem is that for various reasons I may miss some places where the old name appears in an assignment statement such as class_instance.old_attribute_name = something.
For example:
class theClass:
def __init__(self):
self.new_attribute_name = 0
o = theClass()
o.new_attribute_name += 3
if o.new_attribute_name > 3 or o.new_attribute_name < 4:
o.old_attribute_name = 7
do_stuff_with(o.new_attribute_name)
The problem is that this can be difficult to spot, will rise no error and just create a useless attribute.
I considered using __slot__, but feels like forcing my deep-seated C/C++ mindset on Python.
So I would like to ask, what am I doing wrong here? What will I lose if I try (somehow) to prevent attribute creation outside of instantiation? What is the pythonic way to handle stray attribute assignments?