python goto 包,,http://ent
python goto 包,,http://ent
http://entrian.com/goto/
# Example 1: Breaking out from a deeply nested loop:from goto import goto, labelfor i in range(1, 10): for j in range(1, 20): for k in range(1, 30): print i, j, k if k == 3: goto .endlabel .endprint "Finished\n"# Example 2: Restarting a loop:from goto import goto, labellabel .startfor i in range(1, 4): print i if i == 2: try: output = message except NameError: print "Oops - forgot to define ‘message‘! Start again." message = "Hello world" goto .startprint output, "\n"# Example 3: Cleaning up after something fails:from goto import goto, label# Imagine that these are real worker functions.def setUp(): print "setUp"def doFirstTask(): print 1; return Truedef doSecondTask(): print 2; return Truedef doThirdTask(): print 3; return False # This one pretends to fail.def doFourthTask(): print 4; return Truedef cleanUp(): print "cleanUp"# This prints "setUp, 1, 2, 3, cleanUp" - no "4" because doThirdTask fails.def bigFunction1(): setUp() if not doFirstTask(): goto .cleanup if not doSecondTask(): goto .cleanup if not doThirdTask(): goto .cleanup if not doFourthTask(): goto .cleanup label .cleanup cleanUp()bigFunction1()print "bigFunction1 done\n"# Example 4: Using comefrom to let the cleanup code take control itself.from goto import comefrom, labeldef bigFunction2(): setUp() if not doFirstTask(): label .failed if not doSecondTask(): label .failed if not doThirdTask(): label .failed if not doFourthTask(): label .failed comefrom .failed cleanUp()bigFunction2()print "bigFunction2 done\n"# Example 5: Using a computed goto:from goto import goto, labellabel .getinputi = raw_input("Enter either ‘a‘, ‘b‘ or ‘c‘, or any other letter to quit: ")if i in (‘a‘, ‘b‘, ‘c‘): goto *ielse: goto .quitlabel .aprint "You typed ‘a‘"goto .getinputlabel .bprint "You typed ‘b‘"goto .getinputlabel .cprint "You typed ‘c‘"goto .getinputlabel .quitprint "Finished\n"# Example 6: What happens when a label is missing:from goto import goto, labellabel .realgoto .unreal # Raises a MissingLabelError exception.
python goto 包
相关内容
- Python魔法方法,, 1 # -*- c
- Python学习系列(一)(基础入门),, Python入门
- python安装与使用入门,,1、下载python
- python 类,,class NewC
- Python repr() 函数,,描述repr() 函
- python中的文件I/O,,读取键盘输入:raw
- 【Python web 开发】django rest framwork 的token 登录和原理(
- Python+Selenium练习篇之4-利用link text定位元素,,本文介绍
- 使用PyCharm创建并运行一个Python项目,,(1)首先,在欢迎
- Python 爬起数据时 'gbk' codec can't enco
评论关闭