Issue1416
Created on 2007-11-10 05:58 by gvanrossum, last changed 2007-11-10 22:13 by gvanrossum.
| msg57345 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2007-11-10 05:58 |
|
Here's an implementation of the idea I floated recently on python-dev
(Subject: Declaring setters with getters). This implements the kind of
syntax that I believe won over most folks in the end:
@property
def foo(self): ...
@foo.setter
def foo(self, value=None): ...
There are also .getter and .deleter descriptors. This includes the hack
that if you specify a setter but no deleter, the setter is called
without a value argument when attempting to delete something. If the
setter isn't ready for this, a TypeError will be raised, pretty much
just as if no deleter was provided (just with a somewhat worse error
message :-).
I intend to check this into 2.6 and 3.0 unless there is a huge cry of
dismay. Docs will be left to volunteers as always.
|
| msg57354 (view) |
Author: Paul Pogonyshev (_doublep) |
Date: 2007-11-10 15:45 |
|
Looks great (regardless of how this is implemented). I always hated this
def get_foo / def set_foo / foo = property (get_foo, set_foo).
|
| msg57356 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2007-11-10 18:29 |
|
propset2.diff is a new version that improves upon the heuristic for
making the deleter match the setter: when changing setters, if the old
deleter is the same as the old setter, it will replace both the deleter
and setter.
This diff is relative to the 2.6 trunk; it applies to 3.0 too.
|
| msg57358 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2007-11-10 21:16 |
|
propset3.diff removes the hack that makes the deleter equal to the
setter when no separate deleter has been specified. If you want a
single method to be used as setter and deleter, write this:
@foo.setter
@foo.deleter
def foo(self, value=None): ...
|
| msg57359 (view) |
Author: Christian Heimes (christian.heimes) |
Date: 2007-11-10 21:32 |
|
Fixed a typo:
+PyDoc_STRVAR(getter_doc,
+ "Descriptor to change the setter on a property.");
^^^
|
| msg57361 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2007-11-10 22:13 |
|
Checked into trunk as revision 58929.
|
|
| Date |
User |
Action |
Args |
| 2007-11-10 22:13:02 | gvanrossum | set | status: open -> closed resolution: accepted messages:
+ msg57361 |
| 2007-11-10 21:32:40 | christian.heimes | set | files:
+ propset4.diff nosy:
+ christian.heimes messages:
+ msg57359 |
| 2007-11-10 21:16:57 | gvanrossum | set | files:
+ propset3.diff messages:
+ msg57358 |
| 2007-11-10 18:29:03 | gvanrossum | set | files:
+ propset2.diff messages:
+ msg57356 |
| 2007-11-10 15:45:40 | _doublep | set | nosy:
+ _doublep messages:
+ msg57354 |
| 2007-11-10 05:58:10 | gvanrossum | create | |
|