property using a classmethod
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Thu Jul 9 07:59:44 EDT 2009
Emanuele D'Arrigo a écrit :
> Greetings,
>
> today I did something like this:
>
> class MyClass(object):
>
> @classmethod
> 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
> MyClass.myProperty
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
mailing list