这是检测递归代码的简单方法
import inspect
def f(s):
print " " * (len(inspect.stack())-2), '>>', s
if len(s) <= 1:
print " " * (len(inspect.stack())-2), '<<', s
return s
else:
retval = f(f(s[1:])) + s[0] #Note double recursion
print " " * (len(inspect.stack())-2), '<<', retval
return retval
print f('world')
版画
>> world
>> orld
>> rld
>> ld
>> d
<< d
>> d
<< d
<< dl
>> dl
>> l
<< l
>> l
<< l
<< ld
<< ldr
>> ldr
>> dr
>> r
<< r
>> r
<< r
<< rd
>> rd
>> d
<< d
>> d
<< d
<< dr
<< drl
<< drlo
>> drlo
>> rlo
>> lo
>> o
<< o
>> o
<< o
<< ol
>> ol
>> l
<< l
>> l
<< l
<< lo
<< lor
>> lor
>> or
>> r
<< r
>> r
<< r
<< ro
>> ro
>> o
<< o
>> o
<< o
<< or
<< orl
<< orld
<< orldw
orldw