def walk(self, callback, chain): res = callback(self, chain) assert res in (True, False), "Walk function must return boolean, not %r" % (res, ) if not res: return False chain.append(self) self._walk(callback, chain) chain.pop()