1

Here is the function:

def genWords():
    global wordsList; global current_char; global char_seq_cords; global current_sequence

    current_sequence = "%s%s" % (current_sequence, getChar(current_char))
    testable_prefixes =  map( lambda x: ["%s%s" % (current_sequence, getChar(x)), x], possible_chars() )
    valid_seqs = filter(lambda x: linguisticly_possible_seq(x[0]),
                        testable_prefixes)  # 2 length list with char seq and char cords second

    for i in valid_seqs:
        if (u"%s" % current_sequence).lower() in english_tree:
            wordsList.append(current_sequence)
        current_char = i[1]
        char_seq_cords.append(current_char)

        genWords()

    if not valid_seqs:
        wordsList.append(current_sequence)
        return wordsList
print genWords()
print wordsList

I would expect the output from print genWords() to be a list, instead it is None. The reason I am so confused is because the final line print wordsList prints the expected output; additionally, if I insert this line, print wordsList, right before return wordsList, I again get the expected output. I can't seem to figure out why the function returns None?

The src here if you need reference: https://docs.google.com/document/d/1okYiW3jIkZF8HDwpU5Efx32HHkl1hlqcFj0lP6Io0oY/edit?usp=sharing

PVNRT
  • 235
  • 1
  • 4
  • 14

2 Answers2

2

You only reach the return statement (thus returning your words list) if valid_seqs condition is met.

Otherwise, the function "simply returns", with the default None value.

Notice that, in this case, what is being returned is not your empty list (which would definitely not be equivalent to None). Instead, None is what every function gives back if you don't specify another return value.

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
1

if not valid_seqs will return False if valid_seqs is an empty list. gen_words returns None because you have not defined any return value if valid_seqs is empty.

EvenLisle
  • 4,672
  • 3
  • 24
  • 47