[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

There's also a good point made here:

Basically, if you want a read-only attribute, you _can_ create it by
using the built-in property as a decorator:

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())


More information about the Tutor mailing list