Recursive function returning a list

Bruno Desthuilliers onurb at xiludom.gro
Wed Jul 19 12:26:01 EDT 2006


malkarouri at gmail.com wrote:
> Bruno Desthuilliers wrote:
> 
>>Boris Borcic a écrit :
>>
>>>Hello Bruno,
>>>
>>>Bruno Desthuilliers wrote:
>>>
>>>
>>>>Boris Borcic wrote:
>>>>
>>>>
>>>>>>Do you have any ideas?
>>>>>
>>>>>
>>>>>you could use a recursive generator, like
>>>>>
>>>>>def genAllChildren(self) :
>>>>>    for child in self.children :
>>>>>        yield child
>>>>>        for childchild in child.genAllChildren() :
>>>>>            yield childchild
>>>>
>>>>
>>>>
>>>>Or how to *not* address the real problem...
>>>>
>>>>Boris, using a generator may be a pretty good idea, but *not* as a way
>>>>to solve a problem that happens to be a FAQ !-)
>>>>
>>>
>>>Sorry, but I don't understand your reasoning.
>>
>>It's quite simple. The OP's problem is well-known (it's a FAQ), and easy
>>to solve. The righ answer to it is obviously to give a link to the FAQ
>>(or take time to re-explain it for the zillionth time), not to propose a
>>workaround.
>>
>>
>>>How can you exclude that
>>>the OP /may/ find that a generator neatly solves his problem ?
>>
>>I don't exclude it, and explicitly mentioned in whole letters that, I
>>quote, it "may be a pretty good idea". And actually, the OP's problem is
>>really with default values evaluation scheme - something that every
>>Python programmer should know, because there are cases where you cannot
>>solve it with a generator-based solution !-)
>>
>>
>>>The use
>>>of a default value was not an end in itself, was it ?
>>
>>If the OP has other reasons to want to use an accumulator based solution
>>- which we don't know - then the possibility to use a default value is
>>important.
>>
>>
>>>- and the quirks of
>>>default values being FAQ stuff don't change that. Sure if nobody had
>>>covered that aspect, but a couple other posters did...
>>
>>Yes, but you forgot to mention that - and I would not have post any
>>comment on your solution if you had explicitly mentioned the FAQ or
>>these other answers.
>>
>>
>>>Mmmmhhh somehow it feels like if there is any issue here, it is about
>>>defending the credo "there ought to exist only one obvious way to do it"
>>>?...
>>
>>Nope, it's about trying to make sure that anyone googling for a similar
>>problem will notice the canonical solution somehow.
> 
> 
> Sorry, but I kinda agree with Boris here.

On what ?

> Not that I am anybody here,
> really.

Err... Are you you at least ?-)

> If the question is to use an accumulator based solution, then yes, the
> default values answer is definitely the canonical solution.
> If the question is to write a recursive function that returns a list,
> an accumulator based solution and a generator based solution are two
> different ways for doing that.

Note that the generator-based solution doesn't return a list. (And yes,
I know, it's just a matter of wrapping the call to obj.genAllChildrens()
in a list constructor).

> I don't think there is actually a FAQ
> saying you must use the accumulator solution.

Did I say so ? The FAQ I mention is about default values evaluation, and
it's the problem the OP was facing. Please re-read my post more carefully.

> Actually, the accumulator based solution kind of comes to me
> automatically as standard in any programming language, and I believe
> that this was established as standard in python, _before_ the
> introduction of generators.

FWIW, you don't need to pass an accumulator around to solve this problem:

    def getAllChildren(self):
        children = []
        if self.children:
            children.extend(self.children)
            for child in self.children:
                children.extend(child.getAllChildren())
        return children

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list