Constant attributes#

Sometimes it is necessary to forbid the change of an attribute value after the initial assignment. When ubermagutil.typesystem is used, const=True can be passed to any descriptor to forbid the change of an attribute.

[1]:
import ubermagutil.typesystem as ts


@ts.typesystem(name=ts.Typed(expected_type=str, const=True))
class Person:
    def __init__(self, name):
        self.name = name


p = Person(name="Nikola Tesla")

If we attempt to change the value of person.name attribute, AttributeError exception is raised:

[2]:
try:
    p.name = "James Clerk Maxwell"
except AttributeError:
    print("Exception raised.")
Exception raised.