Class property (was: Class methods)

Laszlo Zsolt Nagy gandalf at
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' 
But I could not. This is what I imagined:

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

Usage example:

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

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


More information about the Python-list mailing list