Explain to me what the big deal with tail call optimization is and why Python needs it

前端 未结 4 986
夕颜
夕颜 2021-02-07 03:39

So apparently, there\'s been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he di

4条回答
  •  我在风中等你
    2021-02-07 04:15

    Tail call optimization makes it easier to write recursive functions without worrying about a stack overflow:

    def fac(n, result=1):
            if n > 1:
                    return fac(n - 1, n * result)
            return result
    

    Without tail call optimization, calling this with a big number could overflow the stack.

提交回复
热议问题