Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109702 > unrolled thread
| Started by | Nagy László Zsolt <gandalf@shopzeus.com> |
|---|---|
| First post | 2016-06-09 09:28 +0200 |
| Last post | 2016-06-09 17:52 +1000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Nagy László Zsolt <gandalf@shopzeus.com> |
|---|---|
| Date | 2016-06-09 09:28 +0200 |
| Subject | what is wrong with this property setter |
| Message-ID | <mailman.91.1465457313.2306.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Mark Summerfield <list@qtrac.plus.com> |
|---|---|
| Date | 2016-06-09 00:49 -0700 |
| Message-ID | <6ed933ba-65ff-4160-9f8f-ba56471962c0@googlegroups.com> |
| In reply to | #109702 |
On Thursday, June 9, 2016 at 8:28:47 AM UTC+1, Nagy László Zsolt wrote: > 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 Change the name of the setter from set_parent to parent, i.e., @parent.setter def parent(self, new_parent): ... That works for me on Python 3.4.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-06-09 17:52 +1000 |
| Message-ID | <5759203e$0$11119$c3e8da3@news.astraweb.com> |
| In reply to | #109702 |
On Thursday 09 June 2016 17:28, Nagy László Zsolt wrote:
> class Test:
Are you using Python 3 or 2? In Python 2, property doesn't work correctly with
classes unless they inherit from object (directly or indirectly).
> 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
If you use the "setter" method, you must use the same name, in this case
"parent", for each part of the property.
If you want to give your getters and setters different names, you have to use
the older property API:
class Test(object):
def get_parent(self): ...
def set_parent(self, value): ...
parent = property(get_parent, set_parent)
The reason why your code doesn't work is because of the way decorator syntax is
defined. You have:
@property
def parent(self): ...
which becomes:
parent = property(parent)
Then you have:
@parent.setter
def set_parent(self, new_parent):
which becomes:
set_parent = parent.setter(set_parent)
which leaves you with TWO property objects, not one:
(1) parent has a getter but no setter;
(2) set_parent has a getter and setter (I think).
--
Steve
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web