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.

import ubermagutil.typesystem as ts

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

p = Person(name="Nikola Tesla")

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

try: = "James Clerk Maxwell"
except AttributeError:
    print("Exception raised.")
Exception raised.