property using a classmethod
bruno.42.desthuilliers at websiteburo.invalid
Thu Jul 9 13:59:44 CEST 2009
Emanuele D'Arrigo a écrit :
> today I did something like this:
> class MyClass(object):
> def myClassMethod(self):
<ot> Usually, the first argument of classmethods is named 'cls' </ot>
> print "ham"
> myProperty = property(myClassMethod, None, None)
> As many of you know this doesn't work and returns a TypeError: the
> object passed to the property is not a callable function but a
> classmethod object, which isn't callable at all. So, how do I do this?
> Ultimately all I want is a non-callable class-level attribute
properties *are* class attributes.
> that gives the result of MyClass.myClassMethod().
> Can it be done?
You could write your own custom descriptor. Or just use an additional
level of indirection, ie:
myProperty = property(lambda self: self.myClassMethod())
but since you already have myClassMethod available, I don't see the
point. What problem are you trying to solve exactly ?
More information about the Python-list