Online Book Reader

Home Category

Learning Python - Mark Lutz [482]

By Root 1361 0
Frankly, application libraries tend to be more important in practice than advanced (and to some, esoteric) language features.

On the other hand, if you do need to care about things like Unicode or binary data, have to deal with API-building tools such as descriptors, decorators, and metaclasses, or just want to dig a bit further in general, the next part of the book will help you get started. The larger examples in the final part will also give you a chance to see the concepts you’ve already learned being applied in more realistic ways.

As this is the end of the core material of this book, you get a break on the chapter quiz—just one question this time. As always, though, be sure to work through this part’s closing exercises to cement what you’ve learned in the past few chapters; because the next part is optional reading, this is the final end-of-part exercises session. If you want to see some examples of how what you’ve learned comes together in real scripts drawn from common applications, check out the “solution” to exercise 4 in Appendix B.

Test Your Knowledge: Quiz

(This question is a repeat from the first quiz in Chapter 1—see, I told you it would be easy! :-) Why does “spam” show up in so many Python examples in books and on the Web?

Test Your Knowledge: Answers

Because Python is named after the British comedy group Monty Python (based on surveys I’ve conducted in classes, this is a much-too-well-kept secret in the Python world!). The spam reference comes from a Monty Python skit, where a couple who are trying to order food in a cafeteria keep getting drowned out by a chorus of Vikings singing a song about spam. No, really. And if I could insert an audio clip of that song here, I would....

Test Your Knowledge: Part VII Exercises

As we’ve reached the end of this part of the book, it’s time for a few exception exercises to give you a chance to practice the basics. Exceptions really are simple tools; if you get these, you’ve probably mastered exceptions.

See Part VII, Exceptions and Tools in Appendix B for the solutions.

try/except. Write a function called oops that explicitly raises an IndexError exception when called. Then write another function that calls oops inside a try/except statement to catch the error. What happens if you change oops to raise a KeyError instead of an IndexError? Where do the names KeyError and IndexError come from? (Hint: recall that all unqualified names come from one of four scopes.)

Exception objects and lists. Change the oops function you just wrote to raise an exception you define yourself, called MyError. Identify your exception with a class. Then, extend the try statement in the catcher function to catch this exception and its instance in addition to IndexError, and print the instance you catch.

Error handling. Write a function called safe(func, *args) that runs any function with any number of arguments by using the *name arbitrary arguments call syntax, catches any exception raised while the function runs, and prints the exception using the exc_info call in the sys module. Then use your safe function to run your oops function from exercise 1 or 2. Put safe in a module file called tools.py, and pass it the oops function interactively. What kind of error messages do you get? Finally, expand safe to also print a Python stack trace when an error occurs by calling the built-in print_exc function in the standard traceback module (see the Python library reference manual for details).

Self-study examples. At the end of Appendix B, I’ve included a handful of example scripts developed as group exercises in live Python classes for you to study and run on your own in conjunction with Python’s standard manual set. These are not described, and they use tools in the Python standard library that you’ll have to research on your own. Still, for many readers, it helps to see how the concepts we’ve discussed in this book come together in real programs. If these whet your appetite for more, you can find a wealth of larger and more realistic application-level Python program

Return Main Page Previous Page Next Page

®Online Book Reader