Recursive generator

Ben C spamspam at spam.eggs
Tue Feb 12 17:17:05 EST 2008


On 2008-02-12, Paul Rubin <> wrote:
> Paul Hankin <paul.hankin at gmail.com> writes:
>> def genDescendants(self):
>>     return chain([self], *[child.genDescendants()
>>         for child in self.children])
>
> That is scary.  It generates an in-memory list the size of the
> whole subtree, at every level.  Total memory consumption is maybe
> even quadratic, depending on the tree shape, but even if it's
> only linear, it's way ugly.

This would probably be better (in terms of memory if not beauty):

    def genDescendants(self):
        return chain([self], *(child.genDescendants()
            for child in self.children))



More information about the Python-list mailing list