[Python-Dev] Extended Function syntax
Just van Rossum
just@letterror.com
Tue, 28 Jan 2003 23:41:42 +0100
Guido van Rossum wrote:
> > class Parrot(object):
> > class count(Property):
> > def Get(prop, self):
> > return self._count
> > def Set(prop, self, value):
> > self._count = value
> > def Del(prop, self):
> > self._count = 0
> > count = count('Current parrot count')
> > _count = 0
>
> I'm not sure that this has much to recommend it over the current
> approach. The unused 'prop' argument smells like a remnant of the
> implementation.
OTOH, if the class were merely used as a container (and never
instantiated), it looks fairly decent (and indeed doesn't need a
[filter] modifier, as MWH already suggested):
class Parrot(object):
_count = 0
class count(Property):
"""The count property."""
def __get__(self):
return self._count
def __set__(self, value):
self._count = value
def __del__(self):
self._count = 0
Still, a lot of magic... This is the Property class going with the
above:
class Property(object):
class __metaclass__(type):
def __new__(cls, name, bases, methods):
if not bases or bases[0] == object:
# this is to make the Property class itself...
return type.__new__(cls, name, bases, methods)
return property(methods.get("__get__"),
methods.get("__set__"),
methods.get("__del__"),
methods.get("__doc__"))
Just