what is wrong with this property setter

Nagy László Zsolt gandalf at shopzeus.com
Thu Jun 9 03:28:34 EDT 2016


class Test:
    def __init__(self):
        self._parent = None

    @property
    def parent(self):
        return self._parent

    @parent.setter
    def set_parent(self, new_parent):
        self._parent = new_parent


p, c = Test(), Test()
c.parent = p

>py -3 test.py

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    c.parent = p
AttributeError: can't set attribute

BTW this does work, but it is not that elegant:

class Test:
    def __init__(self):
        self._parent = None

    def get_parent(self):
        return self._parent

    def set_parent(self, new_parent):
        self._parent = new_parent

    parent = property(get_parent, set_parent)


p, c = Test(), Test()
c.parent = p





More information about the Python-list mailing list