<div dir="ltr"><div><div>> An example of the transformation would help here<br><br></div>An example, that detects cycles in a graph, and doesn't do an update if<br>the graph has cycles.<br><br>class prevent_cycles(property):<br>    """<br>        This uses nodes that point to only one other node: if there is a cycle<br>        in the current subgraph, it will detect that within O(n)<br>    """<br>    def __init__(self, value):<br>        super().__init__()<br>        self._value = None<br>        self.__set__(value)<br><br>    def getter(self):<br>        return self._value<br><br>    def setter(self, value):<br>        if not turtle_and_hare(value):<br>            self._value = value<br>        else:<br>            raise Exception("cycle detected, shutting down")<br><br>    def deleter(self):<br>        del self._value<br><br>    def turtle_and_hare(self, other):<br>        """<br>            Generic turtle and hare implementation. true if cycle, false if not.<br>            Returns True if cyclic from this point, false if it is not.<br>        """<br>        turtle = other<br>        hare = other<br>        fieldname = self.__name__<br>        # this assuming that properties have access to their name, but that<br>        # would also be the same as a function.<br><br>        while True:<br>            if hare is None:<br>                return False<br><br>            hare = getattr(hare, fieldname)<br><br>            if hare is None:<br>                return False<br>            if hare is turtle:<br>                return True<br><br>            hare = getattr(hare, fieldname)<br>            turtle = getattr(turtle, fieldname)<br><br>            if hare is turtle:<br>                return True<br><br>class A(object):<br>    def __init__(self, parent):<br>        @prevent_cycles<br>        self.parent = parent<br><br><br>This would prevent cycles from being created in this object A, and would make<br>some highly reusable code. The same can be done for @not_none, etc, to prevent<br>some states which may be unwanted.<br></div></div>