[Python-Dev] Definining properties - a use case for class decorators? (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Oct 17 03:42:02 CEST 2005


Guido van Rossum wrote:

Nick, and everybody else trying to find a "solution" for this "problem", please don't.

Denying that there's a problem isn't going to make it go away. Many people, including me, have the feeling that the standard way of defining properties at the moment leaves something to be desired, for all the same reasons that have led to @-decorators.

However, I agree that trying to keep the accessor method names out of the class namespace isn't necessary, and may not even be desirable. The way I'm defining properties in PyGUI at the moment looks like this:

class C:

 foo = overridable_property('foo', "The foo property")

 def get_foo(self):
   ...

 def set_foo(self, x):
   ...

This has the advantage that the accessor methods can be overridden in subclasses with the expected effect. This is particularly important in PyGUI, where I have a generic class definition which establishes the valid properties and their docstrings, and implementation subclasses for different platforms which supply the accessor methods.

The only wart is the necessity of mentioning the property name twice, once on the lhs and once as an argument. I haven't thought of a good solution to that, yet.

-- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing at canterbury.ac.nz +--------------------------------------+



More information about the Python-Dev mailing list