Recursive function not returning value

Discussion in 'Python' started by Derek Rhodes, Sep 17, 2004.

  1. Derek Rhodes

    Derek Rhodes Guest

    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

    >>>a = test('foobity')

    it's a word
    The conditional worked, see -> ['foobity']
    >>> print a

    None

    </end session>

    What am I missing?

    -derek.
    Derek Rhodes, Sep 17, 2004
    #1
    1. Advertising

  2. Derek Rhodes

    Paul Rubin Guest

    "Derek Rhodes" <> writes:
    > 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])".
    Paul Rubin, Sep 17, 2004
    #2
    1. Advertising

  3. Derek Rhodes wrote:

    > 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
    George Yoshida, Sep 17, 2004
    #3
  4. Derek Rhodes <rhoder <at> worldpath.net> writes:
    > 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
    Steven Bethard, Sep 17, 2004
    #4
  5. Derek Rhodes

    Derek Rhodes Guest

    "Steven Bethard" <> wrote in message
    news:...
    > Derek Rhodes <rhoder <at> worldpath.net> writes:
    >> 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.
    Derek Rhodes, Sep 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabian Steiner

    Recursive function returning a list

    Fabian Steiner, Jul 17, 2006, in forum: Python
    Replies:
    14
    Views:
    505
    Bruno Desthuilliers
    Jul 20, 2006
  2. n00m
    Replies:
    12
    Views:
    1,103
  3. vamsi
    Replies:
    21
    Views:
    2,051
    Keith Thompson
    Mar 9, 2009
  4. Mark Piffer
    Replies:
    9
    Views:
    898
    luserXtrog
    May 15, 2009
  5. rickhg12hs
    Replies:
    7
    Views:
    259
    Terry Reedy
    May 4, 2010
Loading...

Share This Page