Skip to content

classtools.immutable

sikvelsigma edited this page Aug 18, 2022 · 5 revisions

Summery:

  • class ImmutableProperties

Inhereting from ImmutableProperties allows to set attributes of a class only once


Usage:

from pyuseful.classtools.immutable import ImmutableProperties

class Test(ImmutableProperties):
    # change all attributes once their value is not None will raise an error
    # except if the attribute has '__' prefix
    def __init__(self):
        self.a = 1
        self.b = 2
        self.__i = 0
    
    def fun(self):
        self.a = 10
    
    def inc(self):
        self.__i += 1

t = Test()
# t.a = 0 # this will raise an error
# t.fun() # this will also raise an error
t.inc() # this will NOT raise an error

Clone this wiki locally