Python中exit、return、sys.exit()等使用執行個體和區別

來源:互聯網
上載者:User

Python中exit、return、sys.exit()等使用執行個體和區別

   這篇文章主要介紹了Python中exit、return、sys.exit()等使用執行個體和區別,本文是一個實際項目中的總結,需要的朋友可以參考下

  有這樣一道題目: 字串標識符.修改例 6-1 的 idcheck.py 指令碼,使之可以檢測長度為一的標識符,並且可以識別 Python 關鍵字,對後一個要求,你可以使用 keyword 模組(特別是 keyword.kelist)來幫你.

  我最初的代碼是:

  代碼如下:

  #!/usr/bin/env python

  import string

  import keyword

  import sys

  #Get all keyword for python

  #keyword.kwlist

  #['and', 'as', 'assert', 'break', ...]

  keyWords = keyword.kwlist

  #Get all character for identifier

  #string.letters ==> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

  #string.digits ==> '0123456789'

  charForId = string.letters + "_"

  numForId = string.digits

  idInput = raw_input("Input your words,please!")

  if idInput in keyWords:

  print "%s is keyword fot Python!" % idInput

  else:

  lenNum = len(idInput)

  if(1 == lenNum):

  if(idInput in charForId and idInput != "_"):

  print "%s is legal identifier for Python!" % idInput

  else:

  #It's just "_"

  print "%s isn't legal identifier for Python!" % idInput

  else:

  if(idInput[0:1] in charForId):

  legalstring = charForId + numForId

  for item in idInput[1:]:

  if (item not in legalstring):

  print "%s isn't legal identifier for Python!" % idInput

  sys.exit(0)

  print "%s is legal identifier for Python!2" % idInput

  else:

  print "%s isn't legal identifier for Python!3" % idInput

  代碼完畢後,我測試每一條分支,測試到分支時,必須輸入_d4%等包含非法字元的標識符才能進行測試,我最初以為,sys.exit(0)---正常退出指令碼,sys.exit(1)非正常退出指令碼,但是實際情況是/9sys.exit(1),僅輸出返回碼不同):

  代碼如下:

  if (item not in legalstring):

  print "%s isn't legal identifier for Python!" % idInput

  sys.exit(0)

  Input your words,please!_d4%

  _d4% isn't legal identifier for Python!

  Traceback (most recent call last):

  File "E:/python/idcheck.py", line 37, in

  sys.exit(0)

  SystemExit: 0

  >>>

  由此可見,這樣做沒有達到我預期如下輸出的效果,那麼,問題在哪裡呢?在於sys.exit()始終會拋出一個SystemExit異常。

  代碼如下:

  Input your words,please!_d4%

  _d4% isn't legal identifier for Python!

   代碼如下:

  #!/usr/bin/env python

  import string

  import keyword

  import sys

  import traceback

  try:

  #Get all keyword for python

  #keyword.kwlist

  #['and', 'as', 'assert', 'break', ...]

  keyWords = keyword.kwlist

  #Get all character for identifier

  #string.letters ==> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

  #string.digits ==> '0123456789'

  charForId = string.letters + "_"

  numForId = string.digits

  idInput = raw_input("Input your words,please!")

  if idInput in keyWords:

  print "%s is keyword fot Python!" % idInput

  else:

  lenNum = len(idInput)

  if(1 == lenNum):

  if(idInput in charForId and idInput != "_"):

  print "%s is legal identifier for Python!" % idInput

  else:

  #It's just "_"

  print "%s isn't legal identifier for Python!" % idInput

  else:

  if(idInput[0:1] in charForId):

  legalstring = charForId + numForId

  for item in idInput[1:]:

  if (item not in legalstring):

  print "%s isn't legal identifier for Python!" % idInput

  sys.exit()

  print "%s is legal identifier for Python!2" % idInput

  else:

  print "%s isn't legal identifier for Python!3" % idInput

  except SystemExit:

  pass

  except:

  traceback.print_exc()

  上面的代碼擷取sys.exit()拋出的SystemExit異常。

  return:在定義函數時從函數中返回一個函數的傳回值,終止函數的執行。

  exit:下面的代碼中,如果把sys.exit()替換成exit,則exit僅僅跳出離它最近的for迴圈, print "%s is legal identifier for Python!2" % idInput語句會被輸出,這裡,exit的作用類似於break. 但實際上break和exit作用並不同

   代碼如下:

  for item in idInput[1:]:

  if (item not in legalstring):

  print "%s isn't legal identifier for Python!" % idInput

  sys.exit()

  print "%s is legal identifier for Python!2" % idInput

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.