Pugsley
Pugsley

Reputation: 1457

'return' with not-None expression list returns 'None'

I'm currently working on XML (SVG) traversing and have the next problem:
my function always return None after if currNode.parentNode is rootNode:, though, as you can see in the code below, I've explicitly set return to 'flag', which is non-empty string.
The most interesting part here is that print('...') prints three dots, so the problem is exactly there (or at least looks like that).

According to the Python docs: 'If an expression list is present, it is evaluated, else None is substituted.'

So, in short, the question is:
why 'return' doesn't returns 'flag' string, but 'None' instead?

Here's this piece of code:

def relsLookup(currNode, rootNode):
    if currNode.nextSibling is not None:
        currNode = currNode.nextSibling
        return currNode
    elif currNode.nextSibling is None:
        if currNode.parentNode is rootNode:
            flag = 's2s_exception_end_of_svg'
            print('...')
            return flag
        elif currNode.parentNode.localName == 'g':
            if currNode.parentNode.nextSibling is not None:
                print('<--')
                currNode = currNode.parentNode.nextSibling
                return currNode
            elif currNode.parentNode.nextSibling is None:
                print('<--')
                currNode = currNode.parentNode
                relsLookup(currNode, rootNode)
        else:
            flag = 's2s_exception_unknown_parent'
            return flag
    else:
        flag = 's2s_exception_unknown_sibling'
        return flag

Upvotes: 0

Views: 1064

Answers (3)

ncoghlan
ncoghlan

Reputation: 41516

The core problem you are seeing is that a function which "falls off the end" (i.e. reaches the end of the code without returning a value or raising an exception) will implicitly return None to the caller.

Hence the suggestion from Daren to include an explicit return at the end to see what is happening and aix noting the recursive case where you don't actually return anything.

Given the structure of your code, the recursive case is currently broken, since it won't return a value.

Upvotes: 2

NPE
NPE

Reputation: 500673

I suspect the problem is with the following part of your code:

        elif currNode.parentNode.nextSibling is None:
            print('<--')
            currNode = currNode.parentNode
            relsLookup(currNode, rootNode)

Did you mean to say:

        elif currNode.parentNode.nextSibling is None:
            print('<--')
            currNode = currNode.parentNode
            return relsLookup(currNode, rootNode)

?

Upvotes: 6

Daren Thomas
Daren Thomas

Reputation: 70344

According to your listing, there is only one explanation: Either currNode.nextSibling isn't None or it is, but currNode.parentNode is rootNode is False. Set a default return statement at the end of the method / function to test this:

if currNode.nextSibling is None:
    if currNode.parentNode is rootNode:
        flag = 'end_of_svg'
        print('...')
        return flag
    return 'nested if failed'
return 'currNode.nextSibling is not None!!!'

Upvotes: 2

Related Questions