On 4 January 2011 00:56, Guido van Rossum <guido@python.org> wrote:
> On 20110103 13:22, Dirkjan Ochtman wrote:
>> Do you have actual use cases for these?

On Mon, Jan 3, 2011 at 2:06 PM, K. Richard Pixley <rich@noir.com> wrote:
> Yes.  Here's a toy example for abstractclassproperty:

Um, a toy example is pretty much the opposite of a use case. :-(

That said, I am sure there are use cases for static property and class
property -- I've run into them myself.

An example use case for class property: in App Engine, we have a Model
class (it's similar to Django's Model class). A model has a "kind"
which is a string (it's the equivalent of an SQL table name). The kind
is usually the class name but sometimes there's a need to override it.
However once the class is defined it should be considered read-only.
Currently our choices are to make this an instance property (but there
are some situations where we don't have an instance, e.g. when
creating a new instance using a class method); or to make it a class
attribute (but this isn't read-only); or to make it a class method
(which requires the user to write M.kind() instead of M.kind). If I
had class properties I'd use one here.

A class property that can be fetched is very easy to implement. Because of asymmetry in the descriptor protocol I don't think you can create a class property with behaviour on set though (unless you use a metaclass I guess). 

class classproperty(object):
   def __init__(self, function):
       self._function = function
   def __get__(self, instance, owner):
       return self._function(owner)

All the best,


--Guido van Rossum (python.org/~guido)
Python-ideas mailing list


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html