banner
NEWS LETTER

Python防爆栈模板

Scroll down

简介

类似协程, yield函数, return结果.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def calc(n):
if n <= 1:
return n
else:
a = yield calc(n - 1)
return a + 1


def event_loop(s):
stk, last_rst = [s], None
while stk:
try:
func, last_rst = stk[-1].send(last_rst), None
stk.append(func)
except StopIteration as e:
last_rst = e.value
stk.pop()
return last_rst


print(f"Final result: {event_loop(calc(1000000))}")

我很可爱,请给我钱

其他文章
目录导航 置顶
  1. 1. 简介
  2. 2. 代码