[Python-Dev] Declaring setters with getters
Guido van Rossum
guido at python.org
Wed Oct 31 19:05:32 CET 2007
On 10/31/07, skip at pobox.com <skip at pobox.com> wrote:
>
> Guido> I've come up with a relatively unobtrusive pattern for defining
> Guido> setters. Given the following definition:
>
> ...
>
> I'm a pretty naive user of properties, so can you explain to me how what you
> propose is better than
>
> class C(object):
> def __init__(self):
> self._encoding = None
>
> def get_encoding(self):
> return self._encoding
>
> def set_encoding(self, value):
> if value is not None:
> unicode("0", value) # Test it
> self._encoding = value
> encoding = property(get_encoding, set_encoding)
>
> ?
Mostly so it's easy to add a setter to a property that originally had
only a getter. For read-only properties, using
@property
def name(self):
return <whatever>
is preferred over
def get_name(self):
return <whatever>
name = property(get_name)
> Guido> I'd also like to change property so that the doc string defaults
> Guido> to the doc string of the getter.
>
> How about defaulting it to the first argument to property() which has a doc
> string?
I considered that briefly, but decided against it -- it could lead to
odd results if the getter has no docstring and the setter's docstring
is about setting only.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list