Overwriting property-> can't set attribute

Raymond Hettinger python at rcn.com
Fri Aug 22 04:48:32 EDT 2008


On Aug 22, 5:38 am, Gregor Horvath <g... at gregor-horvath.com> wrote:
> why is this code failing?
>
> class B(object):
>      pass
>
> B.testattr = property(lambda s:"hallo")
> b = B()
> b.testattr = "test"

First, property() only works when attached to classes, not instances.
So the assignment should be:  B.testattr = property(...)

Second, the property() call in you example only defines a getter
function (the first argument) and not a setter function (the second
argument) it defaults to a read-only property.  That is why
b.testattr='test' would still fail.

Raymond



More information about the Python-list mailing list