[Python-checkins] python/dist/src/Doc/tut tut.tex,1.194,1.195
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Fri, 11 Jul 2003 18:05:39 -0700
Update of /cvsroot/python/python/dist/src/Doc/tut
In directory sc8-pr-cvs1:/tmp/cvs-serv18655
Modified Files:
tut.tex
Log Message:
SF patch #726751: Clarify docs for except target assignment
Brett found that the tutorial didn't really explain what was happening
with exception targets. Hopefully, this sheds some light on the subject.
Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.194
retrieving revision 1.195
diff -C2 -d -r1.194 -r1.195
*** tut.tex 11 Jul 2003 18:58:11 -0000 1.194
--- tut.tex 12 Jul 2003 01:05:37 -0000 1.195
***************
*** 3319,3333 ****
the exception's \emph{argument}.
The presence and type of the argument depend on the exception type.
! For exception types which have an argument, the except clause may
! specify a variable after the exception name (or list) to receive the
! argument's value, as follows:
\begin{verbatim}
>>> try:
! ... spam()
! ... except NameError, x:
! ... print 'name', x, 'undefined'
! ...
! name spam undefined
\end{verbatim}
--- 3319,3345 ----
the exception's \emph{argument}.
The presence and type of the argument depend on the exception type.
!
! The except clause may specify a variable after the exception name (or list).
! The variable is bound to an exception instance with the arguments stored
! in \code{instance.args}. For convenience, the exception instance
! defines \method{__getitem__} and \method{__str__} so the arguments can
! be accessed or printed directly without having to reference \code{.args}.
\begin{verbatim}
>>> try:
! ... raise Exception('spam', 'eggs')
! ... except Exception, inst:
! ... print type(inst) # the exception instance
! ... print inst.args # arguments stored in .args
! ... print inst # __str__ allows args to printed directly
! ... x, y = inst # __getitem__ allows args to be unpacked directly
! ... print 'x =', x
! ... print 'y =', y
! ...
! <type 'instance'>
! ('spam', 'eggs')
! ('spam', 'eggs')
! x = spam
! y = eggs
\end{verbatim}