3

I would like to iterate through an XML structure. My code does seem to work. I checked the debugger and saw that it reached the return, but the for loop continues.

Looking forward to your advice, thank you!

def get_value(root, item):
    for node in root:
        if node.tag == item:
            return node.tag
        else:
            get_value(node, item)
    return 'Item not found in XML'
6
  • 6
    A loop will not, in any circumstance, continue after reaching a return. You must have seen the previous recursive calls completing. Commented Sep 15, 2020 at 13:43
  • 3
    That is impossible, if a return statement was encountered no further iterations of your for loop would execute. Note that your function is recursive so be mindful of that when stepping through the debugger. Commented Sep 15, 2020 at 13:43
  • 2
    If you're expecting the entire chain of recursive calls to exit on the return, see my answer on this question. Commented Sep 15, 2020 at 13:44
  • 2
    We need a minimal reproducible example to help here. This code isn't even indented properly. And it's unclear what you expect the else: condition to be doing (you make a recursive call and ignore the return value). If you actually reach a return, the function always returns immediately unless: 1) The expression associated with the return triggers an exception (e.g. node has no tag attribute) or 2) There is a with or try/finally involved, in which case with/finally cleanup happens first. Why do you think the loop continues after the return? Commented Sep 15, 2020 at 13:44
  • 2
    It looks you might be returning to previous calls to the function since you appear to be using recursion. There is no circumstance in which any code in a function will continue if it is returned from. Commented Sep 15, 2020 at 13:45

2 Answers 2

4

Here the issue is, you are calling the same function is else statement. So, if it enters in else block, it might go on recursively. Try to amend your code, avoiding calling the function from within. Also, you might need to place return inside for loop, highlighted below

def get_value(root, item):
    for node in root:
        if node.tag == item:
            return node.tag
        else:
            get_value(node, item) ---> This is the problem
        return 'Item not found in XML'
Sign up to request clarification or add additional context in comments.

Comments

4

You forgot a return statement right when you start recursion calls. Meaning it would go on forever until max recurison limit is reached or run out of memory.

change your code like this

def get_value(root, item):
    for node in root:
        if node.tag == item:
            return node.tag
        else:
            return get_value(node, item)
    return 'Item not found in XML'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.