[Python-Dev] read-only properties

David Abrahams dave@boost-consulting.com
08 Oct 2002 11:37:46 -0400


I was just working on some docs for Boost.Python and thought I'd write
down what happens when you try to write to a read-only property (I use
properties to expose C++ data members among other things). So:

    >>> class X(object):
    ...    def get(self): return 'x'
    ...    l = property(get)
    ...
    >>> x = X()
    >>> x.l
    'x'
    >>> x.l = 1
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: can't set attribute

This struck me as a very poor error message, given that mechanism in
Boost.Python v1 used to say:

    Traceback (innermost last):
      File "<string>", line 1, in ?
    AttributeError: 'l' attribute is read-only

Is it worth doing something about this?

-- 
           David Abrahams * Boost Consulting
dave@boost-consulting.com * http://www.boost-consulting.com