More Pythonic?

Jeremy Hylton jeremy at cnri.reston.va.us
Mon Sep 20 11:22:19 EDT 1999


>>>>> "AM" == Aahz Maruch <aahz at netcom.com> writes:

  [I wrote:]
  >>import urllib2
  >>try:
  >>    resp = urllib2.urlopen('http://frob.nitz/')
  >>except urllib2.HTTPError, resp:
  >>    print resp.code, resp.msg

  AM> Got it, thanks.  It's always interesting when you explore a new
  AM> area of the language.  Just to make sure I understand this, am I
  AM> correct in thinking that getting the exception object the way
  AM> you show here is better than using sys.exc_info()?

Absolutely.

When an except clause matches an exception, the target of the except
clause is assigned to the exception's parameter.  The parameter can be 
an arbitrary object, and in the example above the exception object is
also a valid HTTP response.

Jeremy




More information about the Python-list mailing list