Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet58/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   54   55   56   57   58   59   60   61   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Further considerations

Stopping a program

We have mentioned how a program will stop when an error exception occurs, unless we

specifically  try  to  catch  that  error,  and  how  we  can  manually  trigger  an  exception  using

raise.  However,  it  is  also  possible  to  simply  end  a  program  immediately  at  a  particular

point, perhaps the harshest way to control program flow. This kind of intervention is often

not required. Programs in general will simply end after the Python interpreter reaches the

last of the commands, when there is nothing more to execute. Also, we can conditionally

execute  most  of  a  program  inside  an  if  block,  and  thus  skip  straight  to  the  end  of  a

program  if  a  condition  is  not  met.  However,  there  are  some  circumstances  where  a

program has an indefinite loop, perhaps waiting for specific events, which we might want

to terminate in a deliberate manner. A good example of this is a graphical user interface,

where the program does not simply end, but rather enters an event-driven loop whereby it

waits  to  respond  to  the  user  providing  input  like  typing  at  the  keyboard  or

clicking/touching a graphical item on the screen. In this case the program will continue to

run until the user specifically exits the program, maybe by selecting a ‘Quit’ option from a

menu.


In general a Python program is stopped with the exit() function  from  the  standard  sys

module,  which  naturally  must  be  imported  first.  In  the  following  example,  although  we

have  a  loop  which  would  normally  carry  on  indefinitely,  the  exit()  stops  the  program

entirely at the first iteration:




import sys

while True: # Loop indefinitely

sys.exit() # Quit program

print('This never gets executed')

Technically  using  sys.exit  raises  a  SystemExit  exception,  which  in  theory  could  be

intercepted  using  try  and  except,  for  example,  to  run  clean-up  code  (e.g.  deleting

temporary  files  or  saving  session  data  to  disk)  before  re-raising  the  exception  so  the

program  actually  stops.  If  you  really  do  want  a  program  to  stop  without  doing  anything

else and with no chance of interception, then os._exit() can be used instead (taking note of

the underscore and the zero which is an exit code):

import os, sys

try:


sys.exit()

except SystemExit:

print("Exit stopped")

os._exit(0) # Can't be stopped




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   54   55   56   57   58   59   60   61   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish