views:

595

answers:

1

Hi guys,

confirmation = property(_get_confirmation, _set_confirmation)
confirmation.short_description = "Confirmation"

When I try the above I get an Exception I don't quite understand:

AttributeError: 'property' object has no attribute 'short_description'

This was an answer to another question on here but I couldn't comment on it as I don't have enough points or something. :-(

In other tests I've also got this error under similar circumstances:

TypeError: 'property' object has only read-only attributes (assign to .short_description)

Any ideas anybody?

AJ

+1  A: 

The result of property() is an object where you can't add new fields or methods. It's immutable which is why you get the error.

Example how to use property().

[EDIT] As for the answer, you refer to: I think the indentation of the example was completely wrong when you looked at it. This has been fixed, now.

Aaron Digulla
So the response in the link is nonsense? :-S
AJ Ostergaard
I think the indentation is broken.
Aaron Digulla
I don't see how indentation would fix it - let's see what motd says to your comment ...
AJ Ostergaard
I also don't see how identation could fix it. Guess it's just wrong.
Rodrigo
Perhaps poroperty() has chaneged in v2.6?
AJ Ostergaard
@Aaron: What do you mean the indentation has been fixed? How does the indentation change anything? Please explain ... :-S
AJ Ostergaard
Sorry but am I being exceptionally dense here?!? :-S
AJ Ostergaard
AJ: In Python, indentation is everything. When I posted my comment, all code was flush left and property() outside of a class doesn't make sense at all.
Aaron Digulla
Since the indentation has been fixed, the code should compile but it might still not work. I haven't tried it in a Python shell, yet. Sorry.
Aaron Digulla
@Aaron: You're absolutely right about indentation being everything and that property() outside a class being non-sensical - what I can't understand is how indentation can fix attempting to create an attribute on an immutable object. Has "property()" changed in Python 2.6?
AJ Ostergaard
>>> class C(object):... def fet_x():... return 'x'... x = property(fet_x)... x.anattr = 'alpha'... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in CTypeError: 'property' object has only read-only attributes (assign to .anattr)>>>
AJ Ostergaard
@AJ: Nope. My comment has confused you. There are two issues: 1. The example was not correctly indented. 2. After the indentation was fixed, the example in the SO answer is still broken because the result of property() is immutable. Sorry for the confusion.
Aaron Digulla
@Aaron: I see motd has acknowledged that his answer was useless. Thanks for chasing - I was very confused for a while. :)
AJ Ostergaard