[Python-Dev] string-valued fget/fset/fdel for properties
Guido van Rossum
guido at python.org
Mon Nov 24 19:20:22 EST 2003
> Nicodemus <nicodemus at esss.com.br>:
> > Actually, this would introduce a nice feature: allow to easily subclass
> > the functions that are part of the property, without the need to
> > re-create the property in the subclass.
> > class C(object):
> > def get_foo(self):
> > return 'C.foo'
> > c = property('get_foo')
> Now *that* would be useful (it's slightly different from the
> original proposal, as I understood it).
> I wrote a function recently to create properties that work
> like that, and I'm finding it very useful. It would be
> great to have it as a standard feature, either as a part
> of the existing 'property' object, or an alternative one.
> > But I don't agree with Edward that property should accept
> > strings. I think they should just accept functions as of now, but
> > don't store the actual function object, just it's name, and delay
> > the name lookup until it is actually needed.
> No! If all that's being used is the name, then just pass
> the name. Anything else would be pointless and confusing.
> Plus it would allow the new behaviour to coexist with the
> current one: if it's a function, call it, and if it's a
> string, use it as a method name to look up.
This alternate possibility is yet another argument against Edward's
But I think it can be done without using string literals: a metaclass
could scan a class definition for new methods that override functions
used by properties defined in base classes, and automatically create a
new property. If you only want this behavior for selected properties,
you can use a different class instead of 'property'. You could then
also do away with the metaclass, but you'd be back at Nicodemus's
proposal, and that seems to incur too much overhead (we could use
heavy caching, but it would be a bit hairy).
Anyway, all of this can be implemented easily by subclassign property
or by defining your own descriptor class -- there's no magic, just
define __get__ and __set__ (and __delete__ and __doc__, to be
So maybe somebody should implement this for themselves and find out
how often they really use it.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev