python中goto的用法_python3里用goto
python里用goto也是小Pa最近做的項(xiàng)目里的一個(gè)需求。python不像C有自帶的goto, 需要用額外的包,目前為止,小pa只看到2個(gè)goto的包:
這2個(gè)小Pa都下載試用過,goto因?yàn)殚_發(fā)的時(shí)候比較早,對(duì)于python3的支持不太好,不推薦使用,目前推薦使用goto-statement, 這個(gè)project的熱度比較低,但是Forks/Stars的比率來說還是不錯(cuò)的。小Pa給它也貢獻(xiàn)1顆星,第761顆星星就是小Pa點(diǎn)的啦。goto-statement GitHub statistics
不過,用下來基本的功能還是可以的。
當(dāng)然,美中不足的是,goto-statement的最新的版本1.2還無法支持動(dòng)態(tài)的計(jì)算出的label.
goto-statement的貢獻(xiàn)作者Sebastian Noack本人也在和issue的提交者溝通時(shí),做了解釋:Thatgoto .foois technical parsed as attribute lookup is an implementation detail, and sincegetattr(goto, 'foo')results into different bytecode (it's a function call, not an attribute lookup on the bytecode-level), it is ignored when the@with_gotodecorator rewrites the bytecode. Furthermore, in order to support calculated jump targets, we'd have to keep a mapping of names and their destination, and resolve them at runtime. This is not supported at the moment, and might be impossible with the current approach, also considering that the bytecode to perform the jump (with the current approach) cannot be any larger than the original bytecode it replaces. However, one workaround would be, generating a string of Python code and usingexec(with_goto(compile(...)))after resolving all labels.
總之,目前想要在python3里實(shí)現(xiàn)goto, goto-statement是個(gè)不錯(cuò)的選擇,如果你有在實(shí)踐中發(fā)現(xiàn)更好的方法,歡迎交流!
總結(jié)
以上是生活随笔為你收集整理的python中goto的用法_python3里用goto的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信聊天记录云存储多少钱 云储存怎么收费
- 下一篇: websocket python爬虫_p