Class property (was: Class methods)
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Class property (was: Class methods)

From: Laszlo Zsolt Nagy <gandalf@designaproduct.biz>
Date: Thu Oct 06 2005 - 11:05:10 CEST

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
Received on Sat Oct 15 04:12:45 2005