Skip to content

Instantly share code, notes, and snippets.

@freemo
Created December 19, 2019 21:54
Show Gist options
  • Save freemo/c31eb7f28c10db18bc2dcfce0dd75864 to your computer and use it in GitHub Desktop.
Save freemo/c31eb7f28c10db18bc2dcfce0dd75864 to your computer and use it in GitHub Desktop.
class ImmutableMeta(ElementMeta):
def __new__(cls, name, bases, namespace, **kwargs):
def my_setattr(self, name, value):
if hasattr(self, "_immutable") and self._immutable and not name.startswith("__"):
raise AttributeError("this class was set immutable can not set %s, %s" % (name, self))
return object.__setattr__(self, name, value)
namespace["__setattr__"] = my_setattr
def my_freeze(self):
self._immutable = True
namespace["freeze"] = my_freeze
self = super().__new__(cls, name, bases, namespace)
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment