built-in 'property'

Sean Ross sross at connectmail.carleton.ca
Thu Dec 30 22:44:54 CET 2004


"Steven Bethard" <steven.bethard at gmail.com> wrote in message
news:4hhAd.650831$D%.387759 at attbi_s51...
[snip]
> For this reason, I usually suggest declaring properties like[1]:
>
> py> class E(object):
> ...     def x():
> ...         def get(self):
> ...             return float(self._x)
> ...         def set(self, x):
> ...             self._x = x**2
> ...         return dict(fget=get, fset=set)
> ...     x = property(**x())
> ...     def __init__(self, x):
> ...         self._x = x
> ...
[snip]
>
> [1] Thanks to whoever originally suggested this! Sorry, I've forgotten
> who...

Hello.

As Alex mentioned, I'm the author of the "tidy property idiom" recipe. I
stumbled across the idea back in June of 2003, when there were ongoing
discussions about property syntax and, in particular, thunks. I've
discovered that Greg Ewing had a similar idea 6 months earlier [1], though I
wasn't aware of that at the time. I'll note that it is possible to change
the built-in property (in a backward compatible manner) to be used as a
decorator for this idiom, to redefine parts of properties in sub-classes,
and to provide default get/set/del methods. That being said, while I
appreciate that there are people who like this recipe (and others who
don't), I think it's important to point out that this is *not* the
recommended property idiom. Moreover, Guido doesn't like it and he would
prefer that it not become the standard [2][3].

Sean


[1] http://mail.python.org/pipermail/python-dev/2003-January/032611.html
[2] http://mail.python.org/pipermail/python-dev/2003-January/032630.html
[3] http://mail.python.org/pipermail/python-dev/2004-January/042206.html





More information about the Python-list mailing list