Decorator Syntax For Recursive Properties
Jeffrey Froman
jeffrey at fro.man
Sun Apr 17 17:39:00 EDT 2005
Peter Otten wrote:
>> something like this didn't work for me:
<snip>
> But this will, I suppose:
>
> @property
> def ancestors(self):
> if self.parent:
> return self.parent.ancestors + [self.parent]
> return []
>
> A non-recursive variant:
>
> @property
> def ancestors(self):
> result = []
> node = self.parent
> while node:
> result.append(node)
> node = node.parent
> result.reverse()
> return result
Indeed, both of these patterns suit my needs. Thanks very much for the
eye-opening insights.
Jeffrey
More information about the Python-list
mailing list