Recursive function returning a list
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Jul 18 19:16:04 EDT 2006
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.
More information about the Python-list
mailing list