日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

无限“递归”的python程序

發布時間:2023/12/10 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 无限“递归”的python程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  如果一個函數直接或者間接調用了自己,那么就形成了遞歸(recursion),比如斐波那契數列的一個實現

def fib(n):if n <= 2:return 1else:return fib(n - 1) + fib(n - 2)

  遞歸一定要有結束條件,否則就形成了死循環, 比如下面的例子:

def a():b() def b():a()if __name__ == '__main__':a() 很快 就會拋出一個異常:RuntimeError:?maximum?recursion?depth?exceeded 會什么報這個異常,很簡單,我們都知道子程序調用(call)需要壓棧出棧,如果無限遞歸調用,那么就一直壓棧,沒有出棧,內存消耗也越來愈多。python比較高級,直接拋出這個異常,結束程序運行。 前面的文章提到協程(coroutine)這個概念,不管是generator還是greenlet,語法看起來都很像函數調用,但事實上并不是,協程只是在切換的時候把當前調用棧中的信息存儲了起來: “all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the?yield?expression was just another external call.”

?

  利用協程實現無限遞歸似乎成為了可能, 維基百科上有偽代碼描述。首先對于greenlet,實現這個“無限遞歸”函數比較容易的。

1 from greenlet import greenlet 2 def test1(): 3 while True: 4 z = gr2.switch('msg from test1') 5 print('test1 ', z) 6 7 def test2(v): 8 while True: 9 u = gr1.switch('msg from test2') 10 print('test2 ', u) 11 12 if __name__ == '__main__': 13 gr1 = greenlet(test1) 14 gr2 = greenlet(test2) 15 print gr1.switch()

?

接下來用generator來模擬這個實現 def consumer(func):def wrapper(*args,**kw):gen = func(*args, **kw)gen.next()return genwrapper.__name__ = func.__name__wrapper.__dict__ = func.__dict__wrapper.__doc__ = func.__doc__return wrapper@consumer def test1():while True:data = yieldprint('test1 ', data)gr2.send('msg from test1')@consumer def test2():while True:data = yieldprint('test2 ', data)gr1.send('msg from test2')gr1 = test1() gr2 = test2()gr1.send("init")

?

運行報錯:ValueError: generator already executing,這個錯誤在這篇文章也有提到,這個問題,在維基百科上正確的姿勢。我們改改代碼

?

def test1():while True: data = yield (gr2, 'msg from test1')print('test1 ', data)def test2():while True:data = yield (gr1, 'msg from test2')print('test2 ', data)gr1 = test1() gr2 = test2() gr1.next() gr2.next() def run():co, data = gr1, 'init'while True:co, data = co.send(data) run()

This‘s Ok!

references: http://www.cnblogs.com/xybaby/p/6323358.html https://en.wikipedia.org/wiki/Coroutine#Implementations_for_Python https://segmentfault.com/q/1010000003059446

總結

以上是生活随笔為你收集整理的无限“递归”的python程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。