-1

After reading several other posts, it's still not clear to me why my code does work as expected. While I am using the Ansys SpaceClaim API, that shouldn't have any bearing on the recursion logic which is the basis of my question. In essence, I would like to recurse through all the components (iterable) in my structure, find the component I'm looking for by name, and return it when found.

NAME = "SomeComponentName"

def recurse(complist):
    for comp in complist:
        if comp.GetName() == NAME:
            print(comp.GetName())
            return comp
        else:
            recurse(comp.Components) 
                
component = recurse(GetRootPart().Components) 
print(component)

The output is currently something like:

SomeComponentName
None

Which clearly indicates to me that the component was found, yet None is returned. The only thing I can think is that the function is continuing to recurse even after the component is found but why would it continue recursion if the "else" path is not taken?

EDIT: Contrary to the answer in the post linked as the duplicate, changing recurse(comp.Components) to return return recurse(comp.Components) has no effect

Sterling Butters
  • 1,024
  • 3
  • 20
  • 41
  • If it were continuing, you'd eventually get an exception due to a stack overflow. – Ouroborus Aug 23 '23 at 21:37
  • @Ouroborus So then what are some other explanations for what is happening? – Sterling Butters Aug 23 '23 at 21:37
  • 2
    You probably need to change the last line of your function to `return recurse(comp.Components)`. Otherwise, how would the results ever get out of the recursion stack? – Ouroborus Aug 23 '23 at 21:38
  • @SterlingButters you're passing an empty list in for `complist`. So the function just exists. – BTables Aug 23 '23 at 21:39
  • 1
    @BTables No, because clearly they do find something as shown by the `print`. – Ouroborus Aug 23 '23 at 21:40
  • is the current output "something like" that or is it that? – Aiden Cullo Aug 23 '23 at 21:40
  • @Ouroborus yes. But it's recursion. They can have multiple outputs. – BTables Aug 23 '23 at 21:41
  • you should link to the api so one could reproduce this example in it's entirety. see https://stackoverflow.com/help/minimal-reproducible-example – Aiden Cullo Aug 23 '23 at 21:41
  • @BTables Sure, but you said an empty list was being passed in. If the `print` is triggered, then that can't be the case. – Ouroborus Aug 23 '23 at 21:43
  • @Ouroborus passed in after at least one of the recursive calls, thus the `None` output. Obviously not as the initial input.. – BTables Aug 23 '23 at 21:44
  • changing `recurse(comp.Components)` to `return recurse(comp.Components)` has no effect – Sterling Butters Aug 23 '23 at 21:45
  • Obviously you *will* have to `return recurse(..)`, otherwise you will always get `None` from any recursive call. If that still doesn’t work, present us a complete [mre]. – deceze Aug 23 '23 at 21:46
  • 4
    What you need is to replace the `else` clause with `chk = recurse(comp.Components)` / `if chk:` / `return chk`. That way, you'll check ALL the components on the list, and you'll stop looking when you find a hit. – Tim Roberts Aug 23 '23 at 21:53
  • @TimRoberts That worked! Seems related to BTables' comments but this is the actual solution – Sterling Butters Aug 23 '23 at 21:55

0 Answers0