[Python-Dev] string-valued fget/fset/fdel for properties
Edward Loper
edloper at gradient.cis.upenn.edu
Mon Nov 24 00:27:38 EST 2003
I was wondering if there would be any interest for adding a special
case for properties with string-valued fget/fset/fdel:
- if fget is a string, then the getter returns the value of the
member variable with the given name.
- if fset is a string, then the setter sets the value of the
member variable with the given name.
- if fdel is a string, then the deleter deletes the member variable
with the given name.
I.e., the following groups would be functionally equivalant:
property(fget='_foo')
property(fget=lambda self: self._foo)
property(fget=lambda self: getattr(self, '_foo'))
property(fset='_foo')
property(fset=lambda self, value: setattr(self, '_foo', value))
property(fdel='_foo')
property(fdel=lambda self: delattr(self, '_foo'))
This change has 2 advantages:
1. It's easier to read. (In my opinion, anyway; what do other
people think?)
2. It's faster: for properties whose fget/fset/fdel are strings,
we can avoid a function call (since the changes are implemented
in c). Preliminary tests indicate that this results in
approximately a 3x speedup for a tight loop of attribute
lookups. (It's unclear how much of a speed increase you'd get
in actual code, though.)
and one disadvantage (that I can think of):
- It's one more special case to document/know.
This change shouldn't break any existing code, because there's
currently no reason to use string-valued fget/fset/fdel.
Does this change seem useful to other people? Do the advantages
outweigh the disadvantage? Or are there other disadvantage that I
neglected to notice? If this seems like a useful addition, I'd be
happy to work on making a patch that includes test cases & doc
changes.
-Edward
More information about the Python-Dev
mailing list