Recursive function returning a list

malkarouri at gmail.com malkarouri at gmail.com
Tue Jul 18 23:56:55 EDT 2006


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. Not that I am anybody here,
really.

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. I don't think there is actually a FAQ
saying you must use the accumulator solution.
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.

Now, personally I find the generator-based solution more intuitive for
me (in the eys of the beholder:). And, looking at the subject of the
thread, guess what was the question?

k




More information about the Python-list mailing list