This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: @prop.setter decorators
Type: Stage:
Components: Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: _doublep, christian.heimes, gvanrossum
Priority: normal Keywords: patch

Created on 2007-11-10 05:58 by gvanrossum, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
propset.diff gvanrossum, 2007-11-10 05:58
propset2.diff gvanrossum, 2007-11-10 18:29
propset3.diff gvanrossum, 2007-11-10 21:16
propset4.diff christian.heimes, 2007-11-10 21:32
Messages (6)
msg57345 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2007-11-10 22:13
Checked into trunk as revision 58929.
History
Date User Action Args
2022-04-11 14:56:28adminsetgithub: 45757
2007-11-10 22:13:02gvanrossumsetstatus: open -> closed
resolution: accepted
messages: + msg57361
2007-11-10 21:32:40christian.heimessetfiles: + propset4.diff
nosy: + christian.heimes
messages: + msg57359
2007-11-10 21:16:57gvanrossumsetfiles: + propset3.diff
messages: + msg57358
2007-11-10 18:29:03gvanrossumsetfiles: + propset2.diff
messages: + msg57356
2007-11-10 15:45:40_doublepsetnosy: + _doublep
messages: + msg57354
2007-11-10 05:58:10gvanrossumcreate