Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
045612f5
Commit
045612f5
authored
May 12, 1993
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lots of small changes collected over months...
parent
6a3616dc
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
157 additions
and
163 deletions
+157
-163
Doc/Makefile
Doc/Makefile
+1
-1
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+33
-8
Doc/ref/ref5.tex
Doc/ref/ref5.tex
+7
-6
Doc/ref3.tex
Doc/ref3.tex
+33
-8
Doc/ref5.tex
Doc/ref5.tex
+7
-6
Doc/tut.tex
Doc/tut.tex
+38
-67
Doc/tut/tut.tex
Doc/tut/tut.tex
+38
-67
No files found.
Doc/Makefile
View file @
045612f5
...
@@ -31,7 +31,7 @@ qua:
...
@@ -31,7 +31,7 @@ qua:
bibtex qua
bibtex qua
latex qua
latex qua
latex qua
latex qua
dvips
lib
>
qua.ps
dvips
qua
>
qua.ps
libinfo
:
libinfo
:
@
echo
This may take a
while
...
@
echo
This may take a
while
...
...
...
Doc/ref/ref3.tex
View file @
045612f5
...
@@ -290,10 +290,17 @@ There is currently a single mapping type:
...
@@ -290,10 +290,17 @@ There is currently a single mapping type:
\begin{description}
\begin{description}
\item
[Dictionaries]
\item
[Dictionaries]
These represent finite sets of objects indexed by strings.
These represent finite sets of objects indexed by almost arbitrary
values. The only types of values not acceptable as keys are values
containing lists or dictionaries or other mutable types that are
compared by value rather than by object identity --- the reason being
that the implementation requires that a key's hash value be constant.
Numeric types used for keys obey the normal rules for numeric
comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
can be used interchangeably to index the same dictionary entry.
Dictionaries are mutable; they are created by the
\verb
\
{...}
\
Dictionaries are mutable; they are created by the
\verb
\
{...}
\
notation (see section
\ref
{
dict
}
). (Implementation note: the strings
notation (see section
\ref
{
dict
}
).
used for indexing must not contain null bytes.)
\obindex
{
dictionary
}
\obindex
{
dictionary
}
\obindex
{
mutable
}
\obindex
{
mutable
}
...
@@ -409,7 +416,7 @@ base class list.
...
@@ -409,7 +416,7 @@ base class list.
\obindex
{
instance
}
\obindex
{
instance
}
\indexii
{
class object
}{
call
}
\indexii
{
class object
}{
call
}
\index
{
container
}
\index
{
container
}
\index
{
dictionary
}
\
ob
index
{
dictionary
}
\indexii
{
class
}{
attribute
}
\indexii
{
class
}{
attribute
}
Class attribute assignments update the class's dictionary, never the
Class attribute assignments update the class's dictionary, never the
...
@@ -589,12 +596,30 @@ interpretations are used in this case.
...
@@ -589,12 +596,30 @@ interpretations are used in this case.
Called by the
\verb
\
print
\
statement and conversions (reverse quotes) to
Called by the
\verb
\
print
\
statement and conversions (reverse quotes) to
compute the string representation of an object.
compute the string representation of an object.
\item
[\tt _cmp__(self, other)]
\item
[\tt _
_
cmp__(self, other)]
Called by all comparison operations. Should return -1 if
Called by all comparison operations. Should return -1 if
\verb
\
self < other
\
, 0 if
\verb
\
self == other
\
, +1 if
\verb
\
self < other
\
, 0 if
\verb
\
self == other
\
, +1 if
\verb
\
self > other
\
. (Implementation note: due to limitations in the
\verb
\
self > other
\
. If no
\code
{__
cmp
__}
operation is defined, class
interpreter, exceptions raised by comparisons are ignored, and the
instances are compared by object identity (``address'').
objects will be considered equal in this case.)
(Implementation note: due to limitations in the interpreter,
exceptions raised by comparisons are ignored, and the objects will be
considered equal in this case.)
\item
[\tt __hash__(self)]
Called by dictionary operations and by the built-in function
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
for dictionary operations. The only required property is that objects
which compare equal have the same hash value; it is advised to somehow
mix together (e.g. using exclusing or) the hash values for the
components of the object that also play a part in comparison of
objects. If a class does not define a
\code
{__
cmp
__}
method it should
not define a
\code
{__
hash
__}
operation either; if it defines
\code
{__
cmp
__}
but not
\code
{__
hash
__}
its instances will not be
usable as dictionary keys. If a class defines mutable objects and
implements a
\code
{__
cmp
__}
method it should not implement
\code
{__
hash
__}
, since the dictionary implementation assumes that a
key's hash value is a constant.
\obindex
{
dictionary
}
\end{description}
\end{description}
...
...
Doc/ref/ref5.tex
View file @
045612f5
...
@@ -176,8 +176,9 @@ The key/datum pairs are evaluated from left to right to define the
...
@@ -176,8 +176,9 @@ The key/datum pairs are evaluated from left to right to define the
entries of the dictionary: each key object is used as a key into the
entries of the dictionary: each key object is used as a key into the
dictionary to store the corresponding datum.
dictionary to store the corresponding datum.
Keys must be strings, otherwise a
\verb
\
TypeError
\
exception is
Restrictions on the types of the key values are listed earlier in
raised. Clashes between duplicate keys are not detected; the last
section
\ref
{
types
}
.
Clashes between duplicate keys are not detected; the last
datum (textually rightmost in the display) stored for a given key
datum (textually rightmost in the display) stored for a given key
value prevails.
value prevails.
\exindex
{
TypeError
}
\exindex
{
TypeError
}
...
@@ -565,10 +566,10 @@ corresponding items.
...
@@ -565,10 +566,10 @@ corresponding items.
Mappings (dictionaries) are compared through lexicographic
Mappings (dictionaries) are compared through lexicographic
comparison of their sorted (key, value) lists.
%
comparison of their sorted (key, value) lists.
%
\footnote
{
This is expensive since it requires sorting the keys first,
\footnote
{
This is expensive since it requires sorting the keys first,
but about the only sensible definition.
It was tried to compare
but about the only sensible definition.
An earlier version of Python
dictionaries by identity only, but this caused surprises because
compared dictionaries by identity only, but this caused surprises
people expected to be able to test a dictionary for emptiness by
because people expected to be able to test a dictionary for emptiness
comparing it to
{
\tt
\{\}
}
.
}
by
comparing it to
{
\tt
\{\}
}
.
}
\item
\item
Most other types compare unequal unless they are the same object;
Most other types compare unequal unless they are the same object;
...
...
Doc/ref3.tex
View file @
045612f5
...
@@ -290,10 +290,17 @@ There is currently a single mapping type:
...
@@ -290,10 +290,17 @@ There is currently a single mapping type:
\begin{description}
\begin{description}
\item
[Dictionaries]
\item
[Dictionaries]
These represent finite sets of objects indexed by strings.
These represent finite sets of objects indexed by almost arbitrary
values. The only types of values not acceptable as keys are values
containing lists or dictionaries or other mutable types that are
compared by value rather than by object identity --- the reason being
that the implementation requires that a key's hash value be constant.
Numeric types used for keys obey the normal rules for numeric
comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
can be used interchangeably to index the same dictionary entry.
Dictionaries are mutable; they are created by the
\verb
\
{...}
\
Dictionaries are mutable; they are created by the
\verb
\
{...}
\
notation (see section
\ref
{
dict
}
). (Implementation note: the strings
notation (see section
\ref
{
dict
}
).
used for indexing must not contain null bytes.)
\obindex
{
dictionary
}
\obindex
{
dictionary
}
\obindex
{
mutable
}
\obindex
{
mutable
}
...
@@ -409,7 +416,7 @@ base class list.
...
@@ -409,7 +416,7 @@ base class list.
\obindex
{
instance
}
\obindex
{
instance
}
\indexii
{
class object
}{
call
}
\indexii
{
class object
}{
call
}
\index
{
container
}
\index
{
container
}
\index
{
dictionary
}
\
ob
index
{
dictionary
}
\indexii
{
class
}{
attribute
}
\indexii
{
class
}{
attribute
}
Class attribute assignments update the class's dictionary, never the
Class attribute assignments update the class's dictionary, never the
...
@@ -589,12 +596,30 @@ interpretations are used in this case.
...
@@ -589,12 +596,30 @@ interpretations are used in this case.
Called by the
\verb
\
print
\
statement and conversions (reverse quotes) to
Called by the
\verb
\
print
\
statement and conversions (reverse quotes) to
compute the string representation of an object.
compute the string representation of an object.
\item
[\tt _cmp__(self, other)]
\item
[\tt _
_
cmp__(self, other)]
Called by all comparison operations. Should return -1 if
Called by all comparison operations. Should return -1 if
\verb
\
self < other
\
, 0 if
\verb
\
self == other
\
, +1 if
\verb
\
self < other
\
, 0 if
\verb
\
self == other
\
, +1 if
\verb
\
self > other
\
. (Implementation note: due to limitations in the
\verb
\
self > other
\
. If no
\code
{__
cmp
__}
operation is defined, class
interpreter, exceptions raised by comparisons are ignored, and the
instances are compared by object identity (``address'').
objects will be considered equal in this case.)
(Implementation note: due to limitations in the interpreter,
exceptions raised by comparisons are ignored, and the objects will be
considered equal in this case.)
\item
[\tt __hash__(self)]
Called by dictionary operations and by the built-in function
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
for dictionary operations. The only required property is that objects
which compare equal have the same hash value; it is advised to somehow
mix together (e.g. using exclusing or) the hash values for the
components of the object that also play a part in comparison of
objects. If a class does not define a
\code
{__
cmp
__}
method it should
not define a
\code
{__
hash
__}
operation either; if it defines
\code
{__
cmp
__}
but not
\code
{__
hash
__}
its instances will not be
usable as dictionary keys. If a class defines mutable objects and
implements a
\code
{__
cmp
__}
method it should not implement
\code
{__
hash
__}
, since the dictionary implementation assumes that a
key's hash value is a constant.
\obindex
{
dictionary
}
\end{description}
\end{description}
...
...
Doc/ref5.tex
View file @
045612f5
...
@@ -176,8 +176,9 @@ The key/datum pairs are evaluated from left to right to define the
...
@@ -176,8 +176,9 @@ The key/datum pairs are evaluated from left to right to define the
entries of the dictionary: each key object is used as a key into the
entries of the dictionary: each key object is used as a key into the
dictionary to store the corresponding datum.
dictionary to store the corresponding datum.
Keys must be strings, otherwise a
\verb
\
TypeError
\
exception is
Restrictions on the types of the key values are listed earlier in
raised. Clashes between duplicate keys are not detected; the last
section
\ref
{
types
}
.
Clashes between duplicate keys are not detected; the last
datum (textually rightmost in the display) stored for a given key
datum (textually rightmost in the display) stored for a given key
value prevails.
value prevails.
\exindex
{
TypeError
}
\exindex
{
TypeError
}
...
@@ -565,10 +566,10 @@ corresponding items.
...
@@ -565,10 +566,10 @@ corresponding items.
Mappings (dictionaries) are compared through lexicographic
Mappings (dictionaries) are compared through lexicographic
comparison of their sorted (key, value) lists.
%
comparison of their sorted (key, value) lists.
%
\footnote
{
This is expensive since it requires sorting the keys first,
\footnote
{
This is expensive since it requires sorting the keys first,
but about the only sensible definition.
It was tried to compare
but about the only sensible definition.
An earlier version of Python
dictionaries by identity only, but this caused surprises because
compared dictionaries by identity only, but this caused surprises
people expected to be able to test a dictionary for emptiness by
because people expected to be able to test a dictionary for emptiness
comparing it to
{
\tt
\{\}
}
.
}
by
comparing it to
{
\tt
\{\}
}
.
}
\item
\item
Most other types compare unequal unless they are the same object;
Most other types compare unequal unless they are the same object;
...
...
Doc/tut.tex
View file @
045612f5
...
@@ -183,6 +183,12 @@ program will encounter EOF immediately. In the former case (which is
...
@@ -183,6 +183,12 @@ program will encounter EOF immediately. In the former case (which is
usually what you want) they are satisfied from whatever file or device
usually what you want) they are satisfied from whatever file or device
is connected to standard input of the Python interpreter.
is connected to standard input of the Python interpreter.
When a script file is used, it is sometimes useful to be able to run
the script and enter interactive mode afterwards. This can be done by
passing
{
\tt
-i
}
before the script. (This does not work if the script
is read from standard input, for the same reason as explained in the
previous paragraph.)
\subsection
{
Argument Passing
}
\subsection
{
Argument Passing
}
When known to the interpreter, the script name and additional
When known to the interpreter, the script name and additional
...
@@ -211,8 +217,8 @@ and a copyright notice before printing the first prompt, e.g.:
...
@@ -211,8 +217,8 @@ and a copyright notice before printing the first prompt, e.g.:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
python
python
Python 0.9.
7 (Aug 28 1992
).
Python 0.9.
9 (Apr 2 1993
).
Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
Copyright 1990, 1991, 1992
, 1993
Stichting Mathematisch Centrum, Amsterdam
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
...
@@ -1748,58 +1754,44 @@ however, and result in error messages as shown here:
...
@@ -1748,58 +1754,44 @@ however, and result in error messages as shown here:
\bcode\small
\begin{verbatim}
\bcode\small
\begin{verbatim}
>>> 10 * (1/0)
>>> 10 * (1/0)
Unhandled exception: run-time error: integer division by zero
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
ZeroDivisionError: integer division or modulo
>>> 4 + foo*3
>>> 4 + foo*3
Unhandled exception: undefined name: foo
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: foo
>>> '2' + 2
>>> '2' + 2
Unhandled exception: type error: illegal argument type for built-in operation
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
TypeError: illegal argument type for built-in operation
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
The
fir
st line of the error message indicates what happened.
The
la
st line of the error message indicates what happened.
Exceptions come in different types, and the type is printed as part of
Exceptions come in different types, and the type is printed as part of
the message: the types in the example are
the message: the types in the example are
{
\tt
run-time e
rror
}
,
{
\tt
ZeroDivisionE
rror
}
,
{
\tt
undefined name
}
{
\tt
NameError
}
and
and
{
\tt
type error
}
.
{
\tt
TypeError
}
.
The string printed as the exception type is the name of the built-in
name for the exception that occurred. This is true for all built-in
exceptions, but need not be true for user-defined exceptions (although
it is a useful convention).
Standard exception names are built-in identifiers (not reserved
keywords).
The rest of the line is a detail whose interpretation depends on the
The rest of the line is a detail whose interpretation depends on the
exception type.
exception type
; its meaning is dependent on the exception type
.
The
res
t of the error message shows the context where the
The
preceding par
t of the error message shows the context where the
exception happened.
exception happened
, in the form of a stack backtrace
.
In general it contains a stack backtrace listing source lines; however,
In general it contains a stack backtrace listing source lines; however,
it will not display lines read from standard input.
it will not display lines read from standard input.
Here is a summary of the most common exceptions:
The Python library reference manual lists the built-in exceptions and
\begin{itemize}
their meanings.
\item
{
\em
Run-time
\
errors
}
are generally caused by wrong data used by the program; this can be the
programmer's fault or caused by bad input.
The detail states the cause of the error in more detail.
\item
{
\em
Undefined
\
name
}
errors are more serious: these are usually caused by misspelled
identifiers.
%
\footnote
{
The parser does not check whether names used in a program are at
all defined elsewhere in the program; such checks are
postponed until run-time. The same holds for type checking.
}
The detail is the offending identifier.
\item
{
\em
Type
\
errors
}
are also pretty serious: this is another case of
using wrong data (or better, using data the wrong way), but here the
error can be gleaned from the object type(s) alone. The detail shows
in what context the error was detected.
\end{itemize}
\section
{
Handling Exceptions
}
\section
{
Handling Exceptions
}
...
@@ -1813,7 +1805,7 @@ some floating point numbers:
...
@@ -1813,7 +1805,7 @@ some floating point numbers:
... print x,
... print x,
... try:
... try:
... print 1.0 / x
... print 1.0 / x
... except
Runtime
Error:
... except
ZeroDivision
Error:
... print '*** has no inverse ***'
... print '*** has no inverse ***'
...
...
0.3333 3.00030003
0.3333 3.00030003
...
@@ -1862,7 +1854,8 @@ e.g.:
...
@@ -1862,7 +1854,8 @@ e.g.:
%
%
The last except clause may omit the exception name(s), to serve as a
The last except clause may omit the exception name(s), to serve as a
wildcard.
wildcard.
Use this with extreme caution!
Use this with extreme caution, since it is easy to mask a real
programming error in this way!
When an exception occurs, it may have an associated value, also known as
When an exception occurs, it may have an associated value, also known as
the exceptions's
the exceptions's
...
@@ -1882,31 +1875,9 @@ name foo undefined
...
@@ -1882,31 +1875,9 @@ name foo undefined
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
If an exception has an argument, it is printed as the
third
part
If an exception has an argument, it is printed as the
last
part
(`detail') of the message for unhandled exceptions.
(`detail') of the message for unhandled exceptions.
Standard exception names are built-in identifiers (not reserved
keywords).
These are in fact string objects whose
{
\em
object
\
identity
}
(not their value!) identifies the exceptions.
The string is printed as the second part of the message for unhandled
exceptions.
Their names and values are:
\bcode
\begin{verbatim}
EOFError 'end-of-file read'
KeyboardInterrupt 'keyboard interrupt'
MemoryError 'out of memory' *
NameError 'undefined name' *
RuntimeError 'run-time error' *
SystemError 'system error' *
TypeError 'type error' *
\end{verbatim}
\ecode
%
The meanings should be clear enough.
Those exceptions with a
{
\tt
*
}
in the third column have an argument.
Exception handlers don't just handle exceptions if they occur
Exception handlers don't just handle exceptions if they occur
immediately in the try clause, but also if they occur inside functions
immediately in the try clause, but also if they occur inside functions
that are called (even indirectly) in the try clause.
that are called (even indirectly) in the try clause.
...
@@ -1918,10 +1889,10 @@ For example:
...
@@ -1918,10 +1889,10 @@ For example:
...
...
>>> try:
>>> try:
... this
_
fails()
... this
_
fails()
... except
Runtime
Error, detail:
... except
ZeroDivision
Error, detail:
... print 'Handling run-time error:', detail
... print 'Handling run-time error:', detail
...
...
Handling run-time error: integer division
by zer
o
Handling run-time error: integer division
or modul
o
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
...
@@ -1932,10 +1903,10 @@ exception to occur.
...
@@ -1932,10 +1903,10 @@ exception to occur.
For example:
For example:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
>>> raise NameError, 'Hi There!'
>>> raise NameError, 'HiThere'
Unhandled exception: undefined name: Hi There!
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: HiThere
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
@@ -1949,7 +1920,7 @@ variable.
...
@@ -1949,7 +1920,7 @@ variable.
For example:
For example:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
>>> my
_
exc = '
Nobody likes me
'
>>> my
_
exc = '
my
_
exc
'
>>> try:
>>> try:
... raise my
_
exc, 2*2
... raise my
_
exc, 2*2
... except my
_
exc, val:
... except my
_
exc, val:
...
@@ -1957,9 +1928,9 @@ For example:
...
@@ -1957,9 +1928,9 @@ For example:
...
...
My exception occured, value: 4
My exception occured, value: 4
>>> raise my
_
exc, 1
>>> raise my
_
exc, 1
Nobody likes me: 1
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 7
File "<stdin>", line 7
my
_
exc: 1
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
@@ -1979,9 +1950,9 @@ For example:
...
@@ -1979,9 +1950,9 @@ For example:
... print 'Goodbye, world!'
... print 'Goodbye, world!'
...
...
Goodbye, world!
Goodbye, world!
Unhandled exception: keyboard interrupt
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 2
File "<stdin>", line 2
KeyboardInterrupt
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
...
Doc/tut/tut.tex
View file @
045612f5
...
@@ -183,6 +183,12 @@ program will encounter EOF immediately. In the former case (which is
...
@@ -183,6 +183,12 @@ program will encounter EOF immediately. In the former case (which is
usually what you want) they are satisfied from whatever file or device
usually what you want) they are satisfied from whatever file or device
is connected to standard input of the Python interpreter.
is connected to standard input of the Python interpreter.
When a script file is used, it is sometimes useful to be able to run
the script and enter interactive mode afterwards. This can be done by
passing
{
\tt
-i
}
before the script. (This does not work if the script
is read from standard input, for the same reason as explained in the
previous paragraph.)
\subsection
{
Argument Passing
}
\subsection
{
Argument Passing
}
When known to the interpreter, the script name and additional
When known to the interpreter, the script name and additional
...
@@ -211,8 +217,8 @@ and a copyright notice before printing the first prompt, e.g.:
...
@@ -211,8 +217,8 @@ and a copyright notice before printing the first prompt, e.g.:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
python
python
Python 0.9.
7 (Aug 28 1992
).
Python 0.9.
9 (Apr 2 1993
).
Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
Copyright 1990, 1991, 1992
, 1993
Stichting Mathematisch Centrum, Amsterdam
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
...
@@ -1748,58 +1754,44 @@ however, and result in error messages as shown here:
...
@@ -1748,58 +1754,44 @@ however, and result in error messages as shown here:
\bcode\small
\begin{verbatim}
\bcode\small
\begin{verbatim}
>>> 10 * (1/0)
>>> 10 * (1/0)
Unhandled exception: run-time error: integer division by zero
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
ZeroDivisionError: integer division or modulo
>>> 4 + foo*3
>>> 4 + foo*3
Unhandled exception: undefined name: foo
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: foo
>>> '2' + 2
>>> '2' + 2
Unhandled exception: type error: illegal argument type for built-in operation
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
TypeError: illegal argument type for built-in operation
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
The
fir
st line of the error message indicates what happened.
The
la
st line of the error message indicates what happened.
Exceptions come in different types, and the type is printed as part of
Exceptions come in different types, and the type is printed as part of
the message: the types in the example are
the message: the types in the example are
{
\tt
run-time e
rror
}
,
{
\tt
ZeroDivisionE
rror
}
,
{
\tt
undefined name
}
{
\tt
NameError
}
and
and
{
\tt
type error
}
.
{
\tt
TypeError
}
.
The string printed as the exception type is the name of the built-in
name for the exception that occurred. This is true for all built-in
exceptions, but need not be true for user-defined exceptions (although
it is a useful convention).
Standard exception names are built-in identifiers (not reserved
keywords).
The rest of the line is a detail whose interpretation depends on the
The rest of the line is a detail whose interpretation depends on the
exception type.
exception type
; its meaning is dependent on the exception type
.
The
res
t of the error message shows the context where the
The
preceding par
t of the error message shows the context where the
exception happened.
exception happened
, in the form of a stack backtrace
.
In general it contains a stack backtrace listing source lines; however,
In general it contains a stack backtrace listing source lines; however,
it will not display lines read from standard input.
it will not display lines read from standard input.
Here is a summary of the most common exceptions:
The Python library reference manual lists the built-in exceptions and
\begin{itemize}
their meanings.
\item
{
\em
Run-time
\
errors
}
are generally caused by wrong data used by the program; this can be the
programmer's fault or caused by bad input.
The detail states the cause of the error in more detail.
\item
{
\em
Undefined
\
name
}
errors are more serious: these are usually caused by misspelled
identifiers.
%
\footnote
{
The parser does not check whether names used in a program are at
all defined elsewhere in the program; such checks are
postponed until run-time. The same holds for type checking.
}
The detail is the offending identifier.
\item
{
\em
Type
\
errors
}
are also pretty serious: this is another case of
using wrong data (or better, using data the wrong way), but here the
error can be gleaned from the object type(s) alone. The detail shows
in what context the error was detected.
\end{itemize}
\section
{
Handling Exceptions
}
\section
{
Handling Exceptions
}
...
@@ -1813,7 +1805,7 @@ some floating point numbers:
...
@@ -1813,7 +1805,7 @@ some floating point numbers:
... print x,
... print x,
... try:
... try:
... print 1.0 / x
... print 1.0 / x
... except
Runtime
Error:
... except
ZeroDivision
Error:
... print '*** has no inverse ***'
... print '*** has no inverse ***'
...
...
0.3333 3.00030003
0.3333 3.00030003
...
@@ -1862,7 +1854,8 @@ e.g.:
...
@@ -1862,7 +1854,8 @@ e.g.:
%
%
The last except clause may omit the exception name(s), to serve as a
The last except clause may omit the exception name(s), to serve as a
wildcard.
wildcard.
Use this with extreme caution!
Use this with extreme caution, since it is easy to mask a real
programming error in this way!
When an exception occurs, it may have an associated value, also known as
When an exception occurs, it may have an associated value, also known as
the exceptions's
the exceptions's
...
@@ -1882,31 +1875,9 @@ name foo undefined
...
@@ -1882,31 +1875,9 @@ name foo undefined
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
If an exception has an argument, it is printed as the
third
part
If an exception has an argument, it is printed as the
last
part
(`detail') of the message for unhandled exceptions.
(`detail') of the message for unhandled exceptions.
Standard exception names are built-in identifiers (not reserved
keywords).
These are in fact string objects whose
{
\em
object
\
identity
}
(not their value!) identifies the exceptions.
The string is printed as the second part of the message for unhandled
exceptions.
Their names and values are:
\bcode
\begin{verbatim}
EOFError 'end-of-file read'
KeyboardInterrupt 'keyboard interrupt'
MemoryError 'out of memory' *
NameError 'undefined name' *
RuntimeError 'run-time error' *
SystemError 'system error' *
TypeError 'type error' *
\end{verbatim}
\ecode
%
The meanings should be clear enough.
Those exceptions with a
{
\tt
*
}
in the third column have an argument.
Exception handlers don't just handle exceptions if they occur
Exception handlers don't just handle exceptions if they occur
immediately in the try clause, but also if they occur inside functions
immediately in the try clause, but also if they occur inside functions
that are called (even indirectly) in the try clause.
that are called (even indirectly) in the try clause.
...
@@ -1918,10 +1889,10 @@ For example:
...
@@ -1918,10 +1889,10 @@ For example:
...
...
>>> try:
>>> try:
... this
_
fails()
... this
_
fails()
... except
Runtime
Error, detail:
... except
ZeroDivision
Error, detail:
... print 'Handling run-time error:', detail
... print 'Handling run-time error:', detail
...
...
Handling run-time error: integer division
by zer
o
Handling run-time error: integer division
or modul
o
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
...
@@ -1932,10 +1903,10 @@ exception to occur.
...
@@ -1932,10 +1903,10 @@ exception to occur.
For example:
For example:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
>>> raise NameError, 'Hi There!'
>>> raise NameError, 'HiThere'
Unhandled exception: undefined name: Hi There!
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: HiThere
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
@@ -1949,7 +1920,7 @@ variable.
...
@@ -1949,7 +1920,7 @@ variable.
For example:
For example:
\bcode
\begin{verbatim}
\bcode
\begin{verbatim}
>>> my
_
exc = '
Nobody likes me
'
>>> my
_
exc = '
my
_
exc
'
>>> try:
>>> try:
... raise my
_
exc, 2*2
... raise my
_
exc, 2*2
... except my
_
exc, val:
... except my
_
exc, val:
...
@@ -1957,9 +1928,9 @@ For example:
...
@@ -1957,9 +1928,9 @@ For example:
...
...
My exception occured, value: 4
My exception occured, value: 4
>>> raise my
_
exc, 1
>>> raise my
_
exc, 1
Nobody likes me: 1
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 7
File "<stdin>", line 7
my
_
exc: 1
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
@@ -1979,9 +1950,9 @@ For example:
...
@@ -1979,9 +1950,9 @@ For example:
... print 'Goodbye, world!'
... print 'Goodbye, world!'
...
...
Goodbye, world!
Goodbye, world!
Unhandled exception: keyboard interrupt
Stack backtrace (innermost last):
Stack backtrace (innermost last):
File "<stdin>", line 2
File "<stdin>", line 2
KeyboardInterrupt
>>>
>>>
\end{verbatim}
\ecode
\end{verbatim}
\ecode
%
%
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment