Does altering a private member decouple the property's value?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Tue Jun 19 10:23:23 EDT 2007


On Tue, 19 Jun 2007 18:54:41 +1000, Ben Finney wrote:

> "Ethan Kennerly" <kennerly at finegamedesign.com> writes:
> 
>> I really like properties for readonly attributes,
> 
> Python doesn't have "readonly attributes", and to attempt to use
> properties for that purpose will only lead to confusion.


class Parrot(object):
    def _plumage(self):
        return "Beautiful red plumage"
    plumage = property(_plumage)


>>> parrot = Parrot()
>>> parrot.plumage
'Beautiful red plumage'
>>> parrot.plumage = "Ragged grey feathers"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

It walks like a read-only attribute, it squawks like a read-only
attribute, but since Python doesn't have read-only attributes, Ben must be
right: using properties to implement read-only attributes will only lead
to confusion.

*wink*


-- 
Steven.




More information about the Python-list mailing list