SF bug #1076955: Tutorial corrections Part I

(Submitted by some anonymous person with an amazing eye for grammer nits.)
This commit is contained in:
Raymond Hettinger 2004-12-02 06:08:42 +00:00
parent 4d930beeb3
commit aa2b2aa5a3
1 changed files with 67 additions and 54 deletions

View File

@ -33,7 +33,7 @@ on most platforms.
The Python interpreter and the extensive standard library are freely The Python interpreter and the extensive standard library are freely
available in source or binary form for all major platforms from the available in source or binary form for all major platforms from the
Python Web site, \url{http://www.python.org/}, and can be freely Python Web site, \url{http://www.python.org/}, and may be freely
distributed. The same site also contains distributions of and distributed. The same site also contains distributions of and
pointers to many free third party Python modules, programs and tools, pointers to many free third party Python modules, programs and tools,
and additional documentation. and additional documentation.
@ -84,7 +84,7 @@ sufficiently familiar with C.
Another situation: perhaps you have to work with several C libraries, Another situation: perhaps you have to work with several C libraries,
and the usual C write/compile/test/re-compile cycle is too slow. You and the usual C write/compile/test/re-compile cycle is too slow. You
need to develop software more quickly. Possibly perhaps you've need to develop software more quickly. Possibly you've
written a program that could use an extension language, and you don't written a program that could use an extension language, and you don't
want to design a language, write and debug an interpreter for it, then want to design a language, write and debug an interpreter for it, then
tie it into your application. tie it into your application.
@ -103,8 +103,8 @@ in Python as in those languages.
Python allows you to split up your program in modules that can be Python allows you to split up your program in modules that can be
reused in other Python programs. It comes with a large collection of reused in other Python programs. It comes with a large collection of
standard modules that you can use as the basis of your programs --- or standard modules that you can use as the basis of your programs --- or
as examples to start learning to program in Python. There are also as examples to start learning to program in Python. Some of these
built-in modules that provide things like file I/O, system calls, modules provide things like file I/O, system calls,
sockets, and even interfaces to graphical user interface toolkits like Tk. sockets, and even interfaces to graphical user interface toolkits like Tk.
Python is an interpreted language, which can save you considerable time Python is an interpreted language, which can save you considerable time
@ -145,7 +145,7 @@ it is encouraged!
Now that you are all excited about Python, you'll want to examine it Now that you are all excited about Python, you'll want to examine it
in some more detail. Since the best way to learn a language is in some more detail. Since the best way to learn a language is
using it, you are invited here to do so. using it, you are invited to do so with this tutorial.
In the next chapter, the mechanics of using the interpreter are In the next chapter, the mechanics of using the interpreter are
explained. This is rather mundane information, but essential for explained. This is rather mundane information, but essential for
@ -603,7 +603,7 @@ several lines of text just as you would do in C.\n\
print hello print hello
\end{verbatim} \end{verbatim}
Note that newlines would still need to be embedded in the string using Note that newlines still need to be embedded in the string using
\code{\e n}; the newline following the trailing backslash is \code{\e n}; the newline following the trailing backslash is
discarded. This example would print the following: discarded. This example would print the following:
@ -847,7 +847,7 @@ The built-in function \function{len()} returns the length of a string:
Starting with Python 2.0 a new data type for storing text data is Starting with Python 2.0 a new data type for storing text data is
available to the programmer: the Unicode object. It can be used to available to the programmer: the Unicode object. It can be used to
store and manipulate Unicode data (see \url{http://www.unicode.org/}) store and manipulate Unicode data (see \url{http://www.unicode.org/})
and integrates well with the existing string objects providing and integrates well with the existing string objects, providing
auto-conversions where necessary. auto-conversions where necessary.
Unicode has the advantage of providing one ordinal for every character Unicode has the advantage of providing one ordinal for every character
@ -978,8 +978,8 @@ concatenated and so on:
['eggs', 100] ['eggs', 100]
>>> a[:2] + ['bacon', 2*2] >>> a[:2] + ['bacon', 2*2]
['spam', 'eggs', 'bacon', 4] ['spam', 'eggs', 'bacon', 4]
>>> 3*a[:3] + ['Boe!'] >>> 3*a[:3] + ['Boo!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!'] ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
\end{verbatim} \end{verbatim}
Unlike strings, which are \emph{immutable}, it is possible to change Unlike strings, which are \emph{immutable}, it is possible to change
@ -1553,8 +1553,9 @@ TypeError: function() got multiple values for keyword argument 'a'
\end{verbatim} \end{verbatim}
When a final formal parameter of the form \code{**\var{name}} is When a final formal parameter of the form \code{**\var{name}} is
present, it receives a \ulink{dictionary}{../lib/typesmapping.html} containing all keyword arguments present, it receives a \ulink{dictionary}{../lib/typesmapping.html}
whose keyword doesn't correspond to a formal parameter. This may be containing all keyword arguments except for those corresponding to
a formal parameter. This may be
combined with a formal parameter of the form combined with a formal parameter of the form
\code{*\var{name}} (described in the next subsection) which receives a \code{*\var{name}} (described in the next subsection) which receives a
tuple containing the positional arguments beyond the formal parameter tuple containing the positional arguments beyond the formal parameter
@ -1883,8 +1884,8 @@ is shorter than another). For example:
[0, 2, 4, 6, 8, 10, 12, 14] [0, 2, 4, 6, 8, 10, 12, 14]
\end{verbatim} \end{verbatim}
\samp{reduce(\var{func}, \var{sequence})} returns a single value \samp{reduce(\var{function}, \var{sequence})} returns a single value
constructed by calling the binary function \var{func} on the first two constructed by calling the binary function \var{function} on the first two
items of the sequence, then on the result and the next item, and so items of the sequence, then on the result and the next item, and so
on. For example, to compute the sum of the numbers 1 through 10: on. For example, to compute the sum of the numbers 1 through 10:
@ -2174,10 +2175,14 @@ pattern, list comprehensions can compactly specify the key-value list.
\begin{verbatim} \begin{verbatim}
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127} {'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> dict([(x, x**2) for x in vec]) # use a list comprehension >>> dict([(x, x**2) for x in (2, 4, 6)) # use a list comprehension
{2: 4, 4: 16, 6: 36} {2: 4, 4: 16, 6: 36}
\end{verbatim} \end{verbatim}
Later in the tutorial, we will learn about Generator Expressions
which are even better suited for the task of supplying key-values pairs to
the \function{dict()} constructor.
\section{Looping Techniques \label{loopidioms}} \section{Looping Techniques \label{loopidioms}}
@ -2635,7 +2640,7 @@ currently:
>>> import fibo, sys >>> import fibo, sys
>>> fib = fibo.fib >>> fib = fibo.fib
>>> dir() >>> dir()
['__name__', 'a', 'fib', 'fibo', 'sys'] ['__builtins__', '__doc__', '__file__', '__name__', 'fib', 'fib2']
\end{verbatim} \end{verbatim}
Note that it lists all types of names: variables, modules, functions, etc. Note that it lists all types of names: variables, modules, functions, etc.
@ -2647,27 +2652,29 @@ standard module \module{__builtin__}\refbimodindex{__builtin__}:
\begin{verbatim} \begin{verbatim}
>>> import __builtin__ >>> import __builtin__
>>> dir(__builtin__) >>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError', ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
'Exception', 'False', 'FloatingPointError', 'IOError', 'ImportError', 'FloatingPointError', 'FutureWarning', 'IOError', 'ImportError',
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning', 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning',
'PendingDeprecationWarning', 'ReferenceError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',
'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError',
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True',
'True', 'TypeError', 'UnboundLocalError', 'UnicodeError', 'UserWarning', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'ValueError', 'Warning', 'ZeroDivisionError', '__debug__', '__doc__', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'__import__', '__name__', 'abs', 'apply', 'bool', 'buffer', 'UserWarning', 'ValueError', 'Warning', 'WindowsError',
'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', '__name__', 'abs', 'apply', 'basestring', 'bool', 'buffer',
'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile',
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float',
'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex',
'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter',
'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min',
'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range',
'range', 'raw_input', 'reduce', 'reload', 'repr', 'round', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set',
'setattr', 'slice', 'staticmethod', 'str', 'string', 'sum', 'super', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',
'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
\end{verbatim} \end{verbatim}
@ -2824,8 +2831,8 @@ import the three named submodules of the \module{Sound} package.
If \code{__all__} is not defined, the statement \code{from Sound.Effects If \code{__all__} is not defined, the statement \code{from Sound.Effects
import *} does \emph{not} import all submodules from the package import *} does \emph{not} import all submodules from the package
\module{Sound.Effects} into the current namespace; it only ensures that the \module{Sound.Effects} into the current namespace; it only ensures that the
package \module{Sound.Effects} has been imported (possibly running its package \module{Sound.Effects} has been imported (possibly running any
initialization code, \file{__init__.py}) and then imports whatever names are initialization code in \file{__init__.py}) and then imports whatever names are
defined in the package. This includes any names defined (and defined in the package. This includes any names defined (and
submodules explicitly loaded) by \file{__init__.py}. It also includes any submodules explicitly loaded) by \file{__init__.py}. It also includes any
submodules of the package that were explicitly loaded by previous submodules of the package that were explicitly loaded by previous
@ -2907,7 +2914,7 @@ Often you'll want more control over the formatting of your output than
simply printing space-separated values. There are two ways to format simply printing space-separated values. There are two ways to format
your output; the first way is to do all the string handling yourself; your output; the first way is to do all the string handling yourself;
using string slicing and concatenation operations you can create any using string slicing and concatenation operations you can create any
lay-out you can imagine. The standard module layout you can imagine. The standard module
\module{string}\refstmodindex{string} contains some useful operations \module{string}\refstmodindex{string} contains some useful operations
for padding strings to a given column width; these will be discussed for padding strings to a given column width; these will be discussed
shortly. The second way is to use the \code{\%} operator with a shortly. The second way is to use the \code{\%} operator with a
@ -3322,8 +3329,8 @@ it is a useful convention).
Standard exception names are built-in identifiers (not reserved Standard exception names are built-in identifiers (not reserved
keywords). keywords).
The rest of the line is a detail whose interpretation depends on the The rest of the line provides detail based on the type of exception
exception type; its meaning is dependent on the exception type. and what caused it.
The preceding part of the error message shows the context where the The preceding part of the error message shows the context where the
exception happened, in the form of a stack backtrace. exception happened, in the form of a stack backtrace.
@ -3367,9 +3374,8 @@ execution of the \keyword{try} statement is finished.
\item \item
If an exception occurs during execution of the try clause, the rest of If an exception occurs during execution of the try clause, the rest of
the clause is skipped. Then if its type matches the exception named the clause is skipped. Then if its type matches the exception named
after the \keyword{except} keyword, the rest of the try clause is after the \keyword{except} keyword, the except clause is executed, and
skipped, the except clause is executed, and then execution continues then execution continues after the \keyword{try} statement.
after the \keyword{try} statement.
\item \item
If an exception occurs which does not match the exception named in the If an exception occurs which does not match the exception named in the
@ -3480,7 +3486,7 @@ For example:
... except ZeroDivisionError, detail: ... except ZeroDivisionError, detail:
... print 'Handling run-time error:', detail ... print 'Handling run-time error:', detail
... ...
Handling run-time error: integer division or modulo Handling run-time error: integer division or modulo by zero
\end{verbatim} \end{verbatim}
@ -3499,7 +3505,9 @@ NameError: HiThere
The first argument to \keyword{raise} names the exception to be The first argument to \keyword{raise} names the exception to be
raised. The optional second argument specifies the exception's raised. The optional second argument specifies the exception's
argument. argument. Alternatively, the above could be written as
\code{raise NameError('HiThere')}. Either form works fine, but there
seems to be a growing stylistic preference for the latter.
If you need to determine whether an exception was raised but don't If you need to determine whether an exception was raised but don't
intend to handle it, a simpler form of the \keyword{raise} statement intend to handle it, a simpler form of the \keyword{raise} statement
@ -3545,10 +3553,14 @@ Traceback (most recent call last):
__main__.MyError: 'oops!' __main__.MyError: 'oops!'
\end{verbatim} \end{verbatim}
In this example, the default \method{__init__} of \class{Exception} has
been overriden. The new behavior simply creates the \var{value} attribute.
This replaces the default behavior of creating the \var{args} attribute.
Exception classes can be defined which do anything any other class can Exception classes can be defined which do anything any other class can
do, but are usually kept simple, often only offering a number of do, but are usually kept simple, often only offering a number of
attributes that allow information about the error to be extracted by attributes that allow information about the error to be extracted by
handlers for the exception. When creating a module which can raise handlers for the exception. When creating a module that can raise
several distinct errors, a common practice is to create a base class several distinct errors, a common practice is to create a base class
for exceptions defined by that module, and subclass that to create for exceptions defined by that module, and subclass that to create
specific exception classes for different error conditions: specific exception classes for different error conditions:
@ -3623,7 +3635,8 @@ resources (such as files or network connections), regardless of
whether or not the use of the resource was successful. whether or not the use of the resource was successful.
A \keyword{try} statement must either have one or more except clauses A \keyword{try} statement must either have one or more except clauses
or one finally clause, but not both. or one finally clause, but not both (because it would be unclear which
clause should be executed).
\chapter{Classes \label{classes}} \chapter{Classes \label{classes}}
@ -3825,7 +3838,7 @@ When a class definition is left normally (via the end), a \emph{class
object} is created. This is basically a wrapper around the contents object} is created. This is basically a wrapper around the contents
of the namespace created by the class definition; we'll learn more of the namespace created by the class definition; we'll learn more
about class objects in the next section. The original local scope about class objects in the next section. The original local scope
(the one in effect just before the class definitions was entered) is (the one in effect just before the class definitions were entered) is
reinstated, and the class object is bound here to the class name given reinstated, and the class object is bound here to the class name given
in the class definition header (\class{ClassName} in the example). in the class definition header (\class{ClassName} in the example).
@ -3907,9 +3920,9 @@ example,
Now what can we do with instance objects? The only operations Now what can we do with instance objects? The only operations
understood by instance objects are attribute references. There are understood by instance objects are attribute references. There are
two kinds of valid attribute names. two kinds of valid attribute names, data attributes and methods.
The first I'll call \emph{data attributes}. These correspond to \emph{data attributes} correspond to
``instance variables'' in Smalltalk, and to ``data members'' in ``instance variables'' in Smalltalk, and to ``data members'' in
\Cpp. Data attributes need not be declared; like local variables, \Cpp. Data attributes need not be declared; like local variables,
they spring into existence when they are first assigned to. For they spring into existence when they are first assigned to. For
@ -3925,16 +3938,16 @@ print x.counter
del x.counter del x.counter
\end{verbatim} \end{verbatim}
The second kind of attribute references understood by instance objects The other kind of instance attribute references is a \emph{method}.
are \emph{methods}. A method is a function that ``belongs to'' an A method is a function that ``belongs to'' an
object. (In Python, the term method is not unique to class instances: object. (In Python, the term method is not unique to class instances:
other object types can have methods as well. For example, list objects have other object types can have methods as well. For example, list objects have
methods called append, insert, remove, sort, and so on. However, methods called append, insert, remove, sort, and so on. However,
below, we'll use the term method exclusively to mean methods of class in the following discussion, we'll use the term method exclusively to mean
instance objects, unless explicitly stated otherwise.) methods of class instance objects, unless explicitly stated otherwise.)
Valid method names of an instance object depend on its class. By Valid method names of an instance object depend on its class. By
definition, all attributes of a class that are (user-defined) function definition, all attributes of a class that are function
objects define corresponding methods of its instances. So in our objects define corresponding methods of its instances. So in our
example, \code{x.f} is a valid method reference, since example, \code{x.f} is a valid method reference, since
\code{MyClass.f} is a function, but \code{x.i} is not, since \code{MyClass.f} is a function, but \code{x.i} is not, since
@ -4029,12 +4042,12 @@ the readability of methods: there is no chance of confusing local
variables and instance variables when glancing through a method. variables and instance variables when glancing through a method.
Conventionally, the first argument of methods is often called Conventionally, the first argument of a method is often called
\code{self}. This is nothing more than a convention: the name \code{self}. This is nothing more than a convention: the name
\code{self} has absolutely no special meaning to Python. (Note, \code{self} has absolutely no special meaning to Python. (Note,
however, that by not following the convention your code may be less however, that by not following the convention your code may be less
readable by other Python programmers, and it is also conceivable that readable to other Python programmers, and it is also conceivable that
a \emph{class browser} program be written which relies upon such a a \emph{class browser} program might be written that relies upon such a
convention.) convention.)