Hughes, Chad O wrote:
[color=blue]
> Is there any way to create a class method? I can create a class
> variable like this:
>[/color]
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(g et_x,set_x)
Usage example:
[color=blue][color=green][color=darkred]
>>>print A.x[/color][/color][/color]
Getting x
0[color=blue][color=green][color=darkred]
>>>A.x = 8[/color][/color][/color]
Setting x[color=blue][color=green][color=darkred]
>>>print A.x[/color][/color][/color]
Getting x
8
I was trying for a while, but I could not implement a 'classproperty'
function. Is it possible at all?
Thanks,
Les
[color=blue]
> Is there any way to create a class method? I can create a class
> variable like this:
>[/color]
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(g et_x,set_x)
Usage example:
[color=blue][color=green][color=darkred]
>>>print A.x[/color][/color][/color]
Getting x
0[color=blue][color=green][color=darkred]
>>>A.x = 8[/color][/color][/color]
Setting x[color=blue][color=green][color=darkred]
>>>print A.x[/color][/color][/color]
Getting x
8
I was trying for a while, but I could not implement a 'classproperty'
function. Is it possible at all?
Thanks,
Les
Comment