Created
July 24, 2013 11:02
-
-
Save anonymous/6069668 to your computer and use it in GitHub Desktop.
This file contains 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 C(object): | |
... def __init__(self): | |
... self._x = None | |
... @property | |
... def x(self): | |
... return self._x | |
... @x.setter | |
... def set_x(self, value): | |
... self._x = value | |
... @x.deleter | |
... def del_x(self): | |
... del self._x | |
... | |
>>> c = C() | |
>>> c.x | |
>>> c.x = 5 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
AttributeError: can't set attribute | |
>>> c.set_x = 5 | |
>>> c.x | |
5 | |
>>> del c.x | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
AttributeError: can't delete attribute | |
>>> del c.del_x | |
>>> c.x | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "<stdin>", line 6, in x | |
AttributeError: 'C' object has no attribute '_x' | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I miss is why we have a setter only property and a getter only property instead of connecting all the methods to the first property.