Skip to content

Instantly share code, notes, and snippets.

@eclecticmiraclecat
Created May 31, 2020 14:11
Show Gist options
  • Save eclecticmiraclecat/9282c6de8ae038033dc41f164e8f6f49 to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/9282c6de8ae038033dc41f164e8f6f49 to your computer and use it in GitHub Desktop.
>>> class Foo:
... def __init__(self, x):
... self.x = x
... def __setattr__(self, name, value):
... if name not in {'x'}:
... raise AttributeError('only x is allowed')
... super().__setattr__(name, value)
...
>>> f = Foo(2)
>>> f.x
2
>>> f.y = 30
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __setattr__
AttributeError: only x is allowed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment