A goto-like usage of a function

Bengt Richter bokr at oz.net
Thu Jul 29 15:38:38 EDT 2004


On Thu, 29 Jul 2004 13:17:34 -0400, Bart Nessux <bart_nessux at hotmail.com> wrote:

>Peter Hansen wrote:
>> Bart Nessux wrote:
[...]
>> 
>> While it might be okay for a trivial script that is just asking
>> for user input, it is terribly bad style and you would do well
>> to learn to do it differently.
>> 
>> All IMHO, and theoretical discussions of tail recursion
>> notwithstanding.
>> 
>> -Peter
>
>Thanks to all for detailing how loops and 
>recursion differ. I'm going with Tim's loop 
>suggestion now that I realize why I shouldn't use 
>recursion for this type of thing.

OTOH, the default recursion depth limit is 1000, so you could
have gotten 1000 tries before bumping into that.

But there was still an error in your function besides the name collision
that made the recursive call try to call the returned string of raw_input.

The recursive call did not provide for returning a successful result, the
way an immediate successful return did. I.e., the recursive call line
should have been
    return path_name()
not just
    path_name()

BTW, some languages (e.g. scheme) mandate proper tail recursion, so it
can be fine to implement looping via recursion, as Peter is alluding to
(I think ;-) while providing advice for python style which I agree with.

Regards,
Bengt Richter



More information about the Python-list mailing list