Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109702
| From | Nagy László Zsolt <gandalf@shopzeus.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | what is wrong with this property setter |
| Date | 2016-06-09 09:28 +0200 |
| Message-ID | <mailman.91.1465457313.2306.python-list@python.org> (permalink) |
| References | <bc685f14-106c-4d3f-38f9-262e05b896d7@shopzeus.com> |
class Test:
def __init__(self):
self._parent = None
@property
def parent(self):
return self._parent
@parent.setter
def set_parent(self, new_parent):
self._parent = new_parent
p, c = Test(), Test()
c.parent = p
>py -3 test.py
Traceback (most recent call last):
File "test.py", line 15, in <module>
c.parent = p
AttributeError: can't set attribute
BTW this does work, but it is not that elegant:
class Test:
def __init__(self):
self._parent = None
def get_parent(self):
return self._parent
def set_parent(self, new_parent):
self._parent = new_parent
parent = property(get_parent, set_parent)
p, c = Test(), Test()
c.parent = p
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
what is wrong with this property setter Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-09 09:28 +0200 Re: what is wrong with this property setter Mark Summerfield <list@qtrac.plus.com> - 2016-06-09 00:49 -0700 Re: what is wrong with this property setter Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-09 17:52 +1000
csiph-web