How to pass out the result from iterated function

Gary Herron gherron at islandtraining.com
Wed Dec 10 21:26:40 CET 2008


JD wrote:
> I got a iterated function like this:
>
> def iterSomething(list):
>     has_something = False
>     for cell in list:
>         if something in cell:
>             has_something = True
>             output = something
>    if has_something:
>        iterSomething(output)
>    else:
>        final_out = outupt
>
> The problem is how can I read this final_out outside of the function.
> I tried the global statement, it seems not work. Any idea?
>   

Without examining the intent of your code, I'll suggest this:

   if has_something:
       return iterSomething(output)
   else:
       return output

So either way, *something* is returned, and in the case of the recursive call, the innermost result is returned back up through all levels of the recursion.

Is that what you wanted?

Gary Herron






More information about the Python-list mailing list