Class property (was: Class methods)

Laszlo Zsolt Nagy gandalf at designaproduct.biz
Thu Oct 6 11:05:10 CEST 2005


Hughes, Chad O wrote:

> Is there any way to create a class method?  I can create a class 
> variable like this:
>
Hmm, seeing this post, I have decided to implement a 'classproperty' 
descriptor.
But I could not. This is what I imagined:

class A(object):
    _x = 0
    @classmethod
    def get_x(cls):
        print "Getting x..."
        return cls._x
    @classmethod
    def set_x(cls,value):
        print "Setting x..."
        cls._x = value
    x = classproperty(get_x,set_x)

Usage example:

 >>>print A.x
Getting x
0
 >>>A.x = 8
Setting x
 >>>print A.x
Getting x
8

I was trying for a while, but I could not implement a 'classproperty' 
function. Is it possible at all?
Thanks,

   Les







More information about the Python-list mailing list