[Python-Dev] Re: Extended Function syntax
Gerald S. Williams
gsw@agere.com
Tue, 28 Jan 2003 13:01:22 -0500
Just van Rossum wrote:
> With MWH's patch, this could be:
>
> class Foo(object):
> class myprop [property]:
> """A computed property on Foo objects."""
>
> def __get__(self):
> return ...
> def __set__(self):
> ...
> def __delete__(self):
> ...
That doesn't feel right to me. You generally want
self to refer to the Foo object, don't you? With
the class notation, I'd expect self to refer to
Foo.myprop objects. I know they're properties of
the Foo object, but somehow it still seems like a
stretch.
Manual Garcia's recommendation seems cleaner:
> j = block:
> def _get_j(self): return self._j
> def _set_j(self, j): self._j = j
> return property(_get_j, _set_j, None, 'dynamite!')
Although the following idiom works fine for me:
class Parrot(object):
def count():
"Treat mostly-parrots as full parrots."
def Get(self): return self._count
def Set(self,count): self._count = int(round(count))
def Del(self): self._count = 0
return property(Get,Set,Del,"Current parrot count")
count = count()
-Jerry