[Tutor] property built-in
John Fouhy
john at fouhy.net
Fri Oct 20 00:42:47 CEST 2006
On 20/10/06, Kent Johnson <kent37 at tds.net> wrote:
> wesley chun wrote:
> > on a related note, if you're using Python 2.4 and newer, you can
> > simplfy your code a bit by replacing the call to property() with a
> > decorator for x, as in:
> >
> > @property
> > def x():
> > :
>
> I don't think so. This will wrap x itself as a property, equivalent to
> x = property(x)
> which is not the same as Lloyd's code which turns the dict returned by x
> into a property:
> x = property(**x())
There's some discussion in the cookbook about how to make a property
decorator: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698
I'm not sure I'd use it, though --- seems a lot of effort to go to,
when all it would do is trade "foo = property(**foo())" for
"@Property"..
There's also a good point made here:
http://www.artima.com/forums/flat.jsp?forum=122&thread=119914
Basically, if you want a read-only attribute, you _can_ create it by
using the built-in property as a decorator:
@property
def x(self):
return self.__x
equivalent to
def x(self):
return self.__x
x = property(x)
(which works because fget is the first keyword argument for property())
--
John.
More information about the Tutor
mailing list