Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
APPENDIX A ■ THE SHORT VERSION 555 def safe_division(a, b): try: return a/b except ZeroDivisionError: pass ZeroDivisionError is a standard exception. In this case, you could have checked if b was zero, but in many cases, that strategy is not feasible. And besides, if you removed the try/except statement in safe_division, thereby making it a risky function to call (called something like unsafe_division), you could still do the following: try: unsafe_division(a, b) except ZeroDivisionError: print "Something was divided by zero in unsafe_division" In cases in which you typically would not have a specific problem, but it might occur, using exceptions enables you to avoid costly testing and so forth. Well, that’s it. Hope you learned something. Now go and play. And remember the Python motto of learning: “Use the source” (which basically means read all the code you can get your hands on).
- Page 535 and 536: 504 CHAPTER 27 ■ PROJECT 8: FILE
- Page 537 and 538: 506 CHAPTER 27 ■ PROJECT 8: FILE
- Page 539 and 540: 508 CHAPTER 27 ■ PROJECT 8: FILE
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547 and 548: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 549 and 550: 518 CHAPTER 28 ■ PROJECT 9: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553 and 554: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 555 and 556: 524 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 564 and 565: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 566 and 567: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 568 and 569: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 570 and 571: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 572 and 573: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 574 and 575: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 576 and 577: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 578 and 579: APPENDIX A ■ ■ ■ The Short Ve
- Page 580 and 581: APPENDIX A ■ THE SHORT VERSION 54
- Page 582 and 583: APPENDIX A ■ THE SHORT VERSION 55
- Page 584 and 585: APPENDIX A ■ THE SHORT VERSION 55
- Page 588 and 589: APPENDIX B ■ ■ ■ Python Refer
- Page 590 and 591: APPENDIX B ■ PYTHON REFERENCE 559
- Page 592 and 593: APPENDIX B ■ PYTHON REFERENCE 561
- Page 594 and 595: APPENDIX B ■ PYTHON REFERENCE 563
- Page 596 and 597: APPENDIX B ■ PYTHON REFERENCE 565
- Page 598 and 599: APPENDIX B ■ PYTHON REFERENCE 567
- Page 600 and 601: APPENDIX B ■ PYTHON REFERENCE 569
- Page 602 and 603: APPENDIX C ■ ■ ■ Online Resou
- Page 604: APPENDIX C ■ ONLINE RESOURCES 573
- Page 607 and 608: 576 ■INDEX interfaces 155-156 met
- Page 609 and 610: 578 ■INDEX problem 483 second imp
- Page 611 and 612: 580 ■INDEX cookie-cutter code aut
- Page 613 and 614: 582 ■INDEX enumerate function 559
- Page 615 and 616: 584 ■INDEX one line at a time 264
- Page 617 and 618: 586 ■INDEX hasattr function 560 w
- Page 619 and 620: 588 ■INDEX ■J Java Swing GUI to
- Page 621 and 622: 590 ■INDEX mod_python CGI handler
- Page 623 and 624: 592 ■INDEX open function bufferin
- Page 625 and 626: 594 ■INDEX pygame module 528, 573
- Page 627 and 628: 596 ■INDEX regex module importing
- Page 629 and 630: 598 ■INDEX setoutputsize method c
- Page 631 and 632: 600 ■INDEX string methods 59-60 f
- Page 633 and 634: 602 ■INDEX twisted.internet.react
- Page 635: 604 ■INDEX wx.Panel class SetSize
APPENDIX A ■ THE SHORT VERSION 555<br />
def safe_division(a, b):<br />
try:<br />
return a/b<br />
except ZeroDivisionError: pass<br />
ZeroDivisionError is a standard exception. In this case, you could have checked if b was<br />
zero, but in many cases, that strategy is not feasible. And besides, if you removed the try/except<br />
statement in safe_division, thereby making it a risky function <strong>to</strong> call (called something like<br />
unsafe_division), you could still do the following:<br />
try:<br />
unsafe_division(a, b)<br />
except ZeroDivisionError:<br />
print "Something was divided by zero in unsafe_division"<br />
In cases in which you typically would not have a specific problem, but it might occur, using<br />
exceptions enables you <strong>to</strong> avoid costly testing and so forth.<br />
Well, that’s it. Hope you learned something. Now go and play. And remember the <strong>Python</strong><br />
mot<strong>to</strong> of learning: “Use the source” (which basically means read all the code you can get your<br />
hands on).