12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

... print 'got it'...got itThis works because the two strings are mapped to the same object by caching. Incontrast, the strings in the following example are long enough to be outside thecache’s scope:>>> try:... raise 'spam' * 8... except 'spam' * 8:... print 'got it'...Traceback (most recent call last):File "", line 2, in raise 'spam' * 8spamspamspamspamspamspamspamspamIf this seems obscure, all you need to remember is this: for strings, be sure to use thesame object in the raise and the try, not the same value.For class exceptions (the preferred technique today), the overall behavior is similar,but <strong>Python</strong> generalizes the notion of exception matching to include superclassrelationships, so this gotcha doesn’t apply—yet another reason to use class-basedexceptions today!Catching the Wrong ThingPerhaps the most common gotchas related to exceptions involve the design guidelinesdiscussed in the prior section. Remember, try to avoid empty except clauses (oryou may catch things like system exits) and overly specific except clauses (use superclasscategories instead to avoid maintenance issues in the future as new exceptionsare added).Core Language SummaryCongratulations! This concludes your look at the core <strong>Python</strong> programminglanguage. If you’ve gotten this far, you may consider yourself an Official <strong>Python</strong> Programmer(and should feel free to add <strong>Python</strong> to your résumé the next time you dig itout). You’ve already seen just about everything there is to see in the language itself,and all in much more depth than many practicing <strong>Python</strong> programmers initially do.You’ve studied built-in types, statements, and exceptions, as well as tools used tobuild up larger program units (functions, modules, and classes); you’ve evenexplored important design issues, OOP, program architecture, and more.Core Language Summary | 629

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!