Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

Ok, Python doesn't have tail call optimization.

But for those who think better recursively than "looply", whats the best practices to write code??

1000 stack calls are enough for many cases, but what are the tips to conceal recursion with efficiency in Python?

share|improve this question
1  
When you say "conceal" what do you mean? Like hiding the fact that you've created helper functions with different signatures? –  Sean McSomething Oct 22 '13 at 19:48
add comment

marked as duplicate by gnat, GlenH7, MichaelT, Kilian Foth, Robert Harvey Oct 24 '13 at 16:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Well if you're writing tail recursive algorithms, you're probably doing something like this

 def my_algo(whatever)
   if some_condition:
       return foo
   ...
   return my_algo(bar)

Since the call to my_algo is necessarily the last thing to return, it's pretty easy to translate this to

 def my_algo(whatever)
   while some_condition:
       ...
       whatever = bar
   return whatever

This is actually basically what happens with tail call optimization in most compilers anyways.

share|improve this answer
1  
OP also seems to confuse the two, but strictly speaking this is only tail recursion. You can't handle arbitrary tail calls (of the form def f(...): ...; return g(...)) this way. –  delnan Oct 22 '13 at 20:30
    
@delnan Yep, I clarified tail recursion in my answer, but it's a good point that python has a stack and you can't avoid it –  jozefg Oct 22 '13 at 20:33
    
Well, in a sense you are avoiding it with workarounds like the one in this answer. There's a similar (not much uglier, but probably measuably slower) workaround for arbitrary tail calls, trampolining: def f(args): return (g, args) with a wrapper function that repeatedly does (f, args) = f(args) (can be extended to keyword arguments too). –  delnan Oct 22 '13 at 20:37
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.