Recursive function returning a list
- From: Fabian Steiner <lists@xxxxxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 18:27:22 +0200
Hello!
I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind of parent/child relationship.
Now I would like to get all children of a given "Device" object and thought that it would be the best way to use recursive function.
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?
.
- Follow-Ups:
- Re: Recursive function returning a list
- From: Marc 'BlackJack' Rintsch
- Re: Recursive function returning a list
- From: Steve Holden
- Re: Recursive function returning a list
- Prev by Date: Re: Coding style
- Next by Date: Re: Coding style
- Previous by thread: pyKML: where to get it?
- Next by thread: Re: Recursive function returning a list
- Index(es):
Relevant Pages
|