diff --git a/CHANGELOG.md b/CHANGELOG.md index 512182b99114b591ba061693771ecd21a35add9d..c4bad5f3f356b49ca62a8fa178d0797e04f0ae5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # v2.7.16 * extended `Proxy` +* made `Immutable` utilize `__slots__` \ No newline at end of file diff --git a/satella/coding/structures/immutable.py b/satella/coding/structures/immutable.py index cb8690ea77da10d8184444f3b7b91450f9e16006..c529bd8690b2bde6e8d714e627a07181e5484930 100644 --- a/satella/coding/structures/immutable.py +++ b/satella/coding/structures/immutable.py @@ -6,7 +6,7 @@ __all__ = ['Immutable', 'frozendict'] class ImmutableMetaType(ABCMeta): def __call__(cls, *args, **kwargs): p = type.__call__(cls, *args, **kwargs) - p.__dict__['_Immutable__locked_for_writes'] = True + setattr(p, '_Immutable__locked_for_writes', True) return p @@ -21,21 +21,27 @@ class Immutable(metaclass=ImmutableMetaType): >>> self.attribute = 'value' """ - __locked_for_writes = False # type: bool + __slots__ = ('__locked_for_writes', ) # Following make this class immutable def __setattr__(self, attr, value): - if self.__locked_for_writes: - raise TypeError( - '%s does not support attribute assignment' % (self.__class__.__qualname__,)) - else: + try: + if self.__locked_for_writes: + raise TypeError( + '%s does not support attribute assignment' % (self.__class__.__qualname__,)) + else: + super().__setattr__(attr, value) + except AttributeError: super().__setattr__(attr, value) def __delattr__(self, attr): - if self.__locked_for_writes: - raise TypeError( - '%s does not support attribute deletion' % (self.__class__.__qualname__,)) - else: + try: + if self.__locked_for_writes: + raise TypeError( + '%s does not support attribute deletion' % (self.__class__.__qualname__,)) + else: + super().__delattr__(attr) + except AttributeError: super().__delattr__(attr)