I have a class with property decorator.
JavaScript
x
12
12
1
class Dispatcher:
2
def __init__(self, url):
3
self._session = None
4
5
@property
6
def session(self):
7
return self._session
8
9
@session.setter
10
def session(self, value):
11
self._session = value
12
But I don’t need the getter
, can I have property with only the setter
?
It’s most more a curiosity.
Advertisement
Answer
The simplest way IMHO is
JavaScript
1
9
1
class Dispatcher:
2
def __init__(self, url):
3
self._session = None
4
5
def _set_session(self, value):
6
self._session = value
7
8
session = property(fset=_set_session)
9
Demo:
JavaScript
1
10
10
1
>>> d = Dispatcher('a')
2
>>> d.session = 1
3
>>> d._session
4
1
5
>>> d.session
6
Traceback (most recent call last):
7
File "<pyshell#81>", line 1, in <module>
8
d.session
9
AttributeError: unreadable attribute
10