Re: Recursive function returning a list
- From: Marc 'BlackJack' Rintsch <bj_666@xxxxxxx>
- Date: Mon, 17 Jul 2006 19:47:21 +0200
In <e9gdpa$lhb$01$1@xxxxxxxxxxxxxxxxx>, Fabian Steiner wrote:
This is what I got so far:
def getAllChildren(self, children=[]):
if self.children:
children.extend(self.children)
for child in self.children:
child.getAllChildren(children)
return children
Unfortunately, it doesn't work like expected since the default parameter
children=[] is evaluated only once. That's why the children list becomes
larger and larger after calling the method several times but I can't
think of another possibility.
Do you have any ideas?
def get_all_children(self, accumulator=None):
if accumulator is None:
accumulator = list()
accumulator.extend(self.children)
for child in self.children:
child.get_all_children(accumulator)
return accumulator
Ciao,
Marc 'BlackJack' Rintsch
.
- References:
- Recursive function returning a list
- From: Fabian Steiner
- Recursive function returning a list
- Prev by Date: Re: pyKML: where to get it?
- Next by Date: Re: How to lock files (the easiest/best way)?
- Previous by thread: Re: Recursive function returning a list
- Next by thread: using logger module
- Index(es):