On Nov 30, 2015, at 02:32, Steven D'Aprano <steve@pearwood.info> wrote:

On Sun, Nov 29, 2015 at 11:31:14AM -0800, Andrew Barnert via Python-ideas wrote:

(How would you implement a write-only property
that makes "print(spam.eggs)" raise an AttributeError except by
raising it in the property function?)

eggs = property(None, setter, None, "")

makes eggs a write-only property. print(spam.eggs) gives

AttributeError: unreadable attribute

And how do you think property implements that? The Descriptor HOWTO shows a pure-Python equivalent to property, which shows exactly how it works: by raising it in the property __get__ function, of course.

Also notice that it looks exactly the same as if you'd provided an fget argument to property and that function raised (except that way you can control the text of the error).

I think this is a good design. And I think that, except for often-unwanted (but usually-irrelevant) interaction with __getattr__, it's exactly what users would want and expect. But even if that weren't true, it's what Python had done for decades. Which implies that if we are going to gratuitously break it, we need to make it relatively cheap for people whose code depends on the existing behavior to restore it.

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.fget is None:
            raise AttributeError("unreadable attribute")
        return self.fget(obj)