Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2013 11:02
Show Gist options
  • Save anonymous/6069668 to your computer and use it in GitHub Desktop.
Save anonymous/6069668 to your computer and use it in GitHub Desktop.
>>> 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'
>>>
@naufraghi
Copy link

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.

In [20]: C.x.fget?
Type:       function
String Form:<function x at 0x31b06e0>
File:       /home/naufraghi/Documents/comelz/caligola3d.git/src/<ipython-input-8-d2703c8694c3>
Definition: C.x.fget(self)
Docstring:  <no docstring>

In [21]: C.x.fset?
Type:       NoneType
String Form:None
Docstring:  <no docstring>

In [22]: C.set_x.fset?
Type:       function
String Form:<function set_x at 0x31b0668>
File:       /home/naufraghi/Documents/comelz/caligola3d.git/src/<ipython-input-8-d2703c8694c3>
Definition: C.set_x.fset(self, value)
Docstring:  <no docstring>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment