Created
May 31, 2020 14:11
-
-
Save eclecticmiraclecat/9282c6de8ae038033dc41f164e8f6f49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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