Levantando uma exceção ao atualizar um atributo 'constante' em python
Como python não tem o conceito de constantes, seria possível levantar uma exceção se um atributo 'constante' fosse atualizado? Como?
class MyClass():
CLASS_CONSTANT = 'This is a constant'
var = 'This is a not a constant, can be updated'
#this should raise an exception
MyClass.CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'
#this should not raise an exception
MyClass.var = 'updating this is fine'
#this also should raise an exception
MyClass().CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'
#this should not raise an exception
MyClass().var = 'updating this is fine'
Qualquer tentativa de alterar CLASS_CONSTANT como um atributo de classe ou como um atributo de instância deve gerar uma exceção.
Alterar var como um atributo de classe ou como um atributo de instância não deve gerar uma exceção.