Recursive function not returning value

D

Derek Rhodes

using
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32

OK, I have a recursive function that should return a list, but doesn't

<start session>

def test(word):
if type(word) == str:
print "it's a word"
test([word])

if type(word) == list:
print "The conditional worked, see ->", word
return word
it's a word
The conditional worked, see -> ['foobity']None

</end session>

What am I missing?

-derek.
 
P

Paul Rubin

Derek Rhodes said:
if type(word) == str:
print "it's a word"
test([word])

The last line tests [word] and throws away the value. YOu have to say
"return test([word])".
 
G

George Yoshida

Derek said:
OK, I have a recursive function that should return a list, but doesn't

<start session>

def test(word):
if type(word) == str:
print "it's a word"
test([word])

if type(word) == list:
print "The conditional worked, see ->", word
return word

What am I missing?

You are forgetting to return the value.

change this part ::

if type(word) == str:
print "it's a word"
test([word])

to

if type(word) == str:
print "it's a word"
return test([word]) # return the result of test([word])


George
 
S

Steven Bethard

Derek Rhodes said:
OK, I have a recursive function that should return a list, but doesn't

def test(word):
if type(word) == str:
print "it's a word"
test([word])

if type(word) == list:
print "The conditional worked, see ->", word
return word

By default, if a Python function does not hit a return statement before the
end of the function, it returns the None value. Notice that if word is a str,
your function executes the first if-block, including the recursive call and
then skips the second if-block. So in this case, you never hit a return
statement and so Python returns None. You probably meant to write:

def test(word):
if type(word) == str:
return test([word])
if type(word) == list:
return word

If you run into these kind of mistakes frequenly, it might be worth having
only one return point in each function. You would then write your code
something like:

def test(word):
if isinstance(word, str):
result = test([word])
elif isinstance(word, list):
result = word
else:
raise TypeError('unsupported type %r' % type(word))
return result

Of course, this particular example probably doesn't merit a recursive function
anyway, but you get the idea...

Steve
 
D

Derek Rhodes

Steven Bethard said:
Derek Rhodes said:
OK, I have a recursive function that should return a list, but doesn't

def test(word):
if type(word) == str:
print "it's a word"
test([word])

if type(word) == list:
print "The conditional worked, see ->", word
return word

By default, if a Python function does not hit a return statement before
the
end of the function, it returns the None value. Notice that if word is a
str,
your function executes the first if-block, including the recursive call
and
then skips the second if-block. So in this case, you never hit a return
statement and so Python returns None. You probably meant to write:

def test(word):
if type(word) == str:
return test([word])
if type(word) == list:
return word

If you run into these kind of mistakes frequenly, it might be worth having
only one return point in each function. You would then write your code
something like:

def test(word):
if isinstance(word, str):
result = test([word])
elif isinstance(word, list):
result = word
else:
raise TypeError('unsupported type %r' % type(word))
return result

Of course, this particular example probably doesn't merit a recursive
function
anyway, but you get the idea...

Steve

WOW, thanks everyone for the quick reply!

-Derek.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top