I am trying to build all paths recursively in a class. Here is what I have so far:
def get_paths(self, component=None, current_path=None, all_paths=None):
# set defaults
if component is None:
component = self._ground;
current_path = [component,]
##### [START] RECURSIVE PART #####
# get parents of component
parents = component.parents()
# if no parents (an endpoint/leaf), add the current_path
if not parents:
all_paths.append(current_path)
# if parents ==> recurse
# note: because we're starting from the ground and getting all parents
# we insert the parent before the current path (not after, like if we
# were recursively getting files in a directory)
else:
for parent in parents:
self.get_paths(parent, [parent,] + current_path), all_paths)
##### [END] RECURSIVE PART #####
# Note that the recursion doesn't 'return' anything, it only modifies
# the list. We still have to return the list by the method at the end.
return all_paths
What this does is it starts at the 'ground' and then recurses up until the element does not have any parents. My question is whether this is a common way to do recursion -- to not actually return anything in the 'recursive part' but just to modify the mutable element (the list here) and then return the result later.
If the above isn't ideal, what would be an example of how it can be improved? Or, what are other ways in which to return a list of paths (the above is very similar to what the $ find ./ would do getting a list of paths).
all_pathswill be defined once when you define the function and then subsequent calls to the function will modify that list. Effectively you can only call the function once. You need to set it toNoneand then check for it and set the result to[]in the function body.[]instead ofNone. Thank you. Also, I updated the question with that change, as I'm looking more for an answer to the general structure/function of modifying the elements