0

I have a strange problem with the return of a recursive function. It always return 'None' instead of the temp_blocks. He will write the temp_blocks (List of blocks, not None) in the else-case, but it seems it doesn't return the list if i call the function blocks = _searchblocks(roots, left_edge, right_edge). Is this a common problem, or is it my fault?

def _searchblocks(blocks, left_edge, right_edge):
    temp_blocks = []
    for block in blocks:
        if np.any(block.left_edge >= left_edge) \
        and np.any(block.right_edge <= right_edge):
            temp_blocks.append(block)

    if len(temp_blocks) == 1:
        _searchblocks(temp_blocks[0].children, left_edge, right_edge)
    else:
        print(temp_blocks)
        return temp_blocks
Christian
  • 739
  • 1
  • 5
  • 15

1 Answers1

8

You need an explicit return statement:

if len(temp_blocks) == 1:
    return _searchblocks(temp_blocks[0].children, left_edge, right_edge)
    ^^^^^^

Without this, your function calls itself recursively, discards the result and then implicitly returns None.

NPE
  • 486,780
  • 108
  • 951
  • 1,012