Recursive function not returning value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Rhodes

    #1

    Recursive function not returning value

    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
    [color=blue][color=green][color=darkred]
    >>>a = test('foobity')[/color][/color][/color]
    it's a word
    The conditional worked, see -> ['foobity'][color=blue][color=green][color=darkred]
    >>> print a[/color][/color][/color]
    None

    </end session>

    What am I missing?

    -derek.


  • Paul Rubin

    #2
    Re: Recursive function not returning value

    "Derek Rhodes" <rhoder@worldpa th.net> writes:[color=blue]
    > if type(word) == str:
    > print "it's a word"
    > test([word])[/color]

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

    Comment

    • Steven Bethard

      #3
      Re: Recursive function not returning value

      Derek Rhodes <rhoder <at> worldpath.net> writes:[color=blue]
      > 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[/color]

      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('unsu pported 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


      Comment

      • George Yoshida

        #4
        Re: Recursive function not returning value

        Derek Rhodes wrote:
        [color=blue]
        > 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?[/color]

        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

        Comment

        • Derek Rhodes

          #5
          Re: Recursive function not returning value


          "Steven Bethard" <steven.bethard @gmail.com> wrote in message
          news:mailman.34 39.1095406129.5 135.python-list@python.org ...[color=blue]
          > Derek Rhodes <rhoder <at> worldpath.net> writes:[color=green]
          >> 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[/color]
          >
          > 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('unsu pported 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
          >
          >[/color]

          WOW, thanks everyone for the quick reply!

          -Derek.


          Comment

          Working...