what is wrong with this property setter

Peter Otten __peter__ at web.de
Thu Jun 9 05:00:45 EDT 2016


Nagy László Zsolt wrote:

> 
>>>     @parent.setter
>>>     def set_parent(self, new_parent):
>>>         self._parent = new_parent
>> This creates a settable property with the name "set_parent" and leaves
>> the read-only property "parent" alone.

> Yes, and more. That property will also have a get method! Is it
> intentional?

It's a logical effect of how the setter() method works. The above is 
syntactic sugar for

def set_parent(...):
   ...
set_parent = parent.setter(set_parent)

and parent.setter() creates a new property basically like this

def setter(self, fset):
    return property(self.fget, fset, ...)

Not very elegant, but I don't see a cleaner alternative.



More information about the Python-list mailing list