Kaydet (Commit) 65a350d7 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

SF bug #1076955: Tutorial corrections Part II

üst aa2b2aa5
...@@ -40,7 +40,7 @@ Any class which does not inherit from \class{object}. See ...@@ -40,7 +40,7 @@ Any class which does not inherit from \class{object}. See
The implicit conversion of an instance of one type to another during an The implicit conversion of an instance of one type to another during an
operation which involves two arguments of the same type. For example, operation which involves two arguments of the same type. For example,
{}\code{int(3.15)} converts the floating point number to the integer, {}\code{int(3.15)} converts the floating point number to the integer
{}\code{3}, but in {}\code{3+4.5}, each argument is of a different type (one {}\code{3}, but in {}\code{3+4.5}, each argument is of a different type (one
int, one float), and both must be converted to the same type before they can int, one float), and both must be converted to the same type before they can
be added or it will raise a {}\code{TypeError}. Coercion between two be added or it will raise a {}\code{TypeError}. Coercion between two
...@@ -169,7 +169,7 @@ sophisticated, multi-platform GUI application. ...@@ -169,7 +169,7 @@ sophisticated, multi-platform GUI application.
An object with fixed value. Immutable objects are numbers, strings or An object with fixed value. Immutable objects are numbers, strings or
tuples (and more). Such an object cannot be altered. A new object tuples (and more). Such an object cannot be altered. A new object
has to be created if a different value has to be stored. They play an has to be created if a different value has to be stored. They play an
important role in places where a constant hash value is needed. For important role in places where a constant hash value is needed, for
example as a key in a dictionary. example as a key in a dictionary.
\index{integer division} \index{integer division}
...@@ -189,7 +189,7 @@ operator. See also \emph{__future__}. ...@@ -189,7 +189,7 @@ operator. See also \emph{__future__}.
\index{interactive} \index{interactive}
\item[interactive] \item[interactive]
Python has an interactive interpreter which means that you can try out Python has an interactive interpreter which means that you can try out
things and directly see its result. Just launch \code{python} with no things and immediately see their results. Just launch \code{python} with no
arguments (possibly by selecting it from your computer's main menu). arguments (possibly by selecting it from your computer's main menu).
It is a very powerful way to test out new ideas or inspect modules and It is a very powerful way to test out new ideas or inspect modules and
packages (remember \code{help(x)}). packages (remember \code{help(x)}).
...@@ -235,7 +235,7 @@ code that attempts multiple iteration passes. A container object ...@@ -235,7 +235,7 @@ code that attempts multiple iteration passes. A container object
(such as a \class{list}) produces a fresh new iterator each time you (such as a \class{list}) produces a fresh new iterator each time you
pass it to the \function{iter()} function or use it in a pass it to the \function{iter()} function or use it in a
{}\keyword{for} loop. Attempting this with an iterator will just {}\keyword{for} loop. Attempting this with an iterator will just
return the same exhausted iterator object from the second iteration return the same exhausted iterator object used in the previous iteration
pass, making it appear like an empty container. pass, making it appear like an empty container.
\index{list comprehension} \index{list comprehension}
...@@ -245,7 +245,15 @@ return a list with the results. \code{result = ["0x\%02x" ...@@ -245,7 +245,15 @@ return a list with the results. \code{result = ["0x\%02x"
\% x for x in range(256) if x \% 2 == 0]} generates a list of strings \% x for x in range(256) if x \% 2 == 0]} generates a list of strings
containing hex numbers (0x..) that are even and in the range from 0 to 255. containing hex numbers (0x..) that are even and in the range from 0 to 255.
The \keyword{if} clause is optional. If omitted, all elements in The \keyword{if} clause is optional. If omitted, all elements in
{}\code{range(256)} are processed in that case. {}\code{range(256)} are processed.
\index{LBYL}
\item[LBYL]
Look before you leap. This coding style explicitly tests for
pre-conditions before making calls or lookups. This style contrasts
with the \emph{EAFP} approach and is characterized by the presence of
many \keyword{if} statements.
\index{mapping} \index{mapping}
\item[mapping] \item[mapping]
...@@ -265,13 +273,6 @@ have been used for logging attribute access, adding thread-safety, ...@@ -265,13 +273,6 @@ have been used for logging attribute access, adding thread-safety,
tracking object creation, implementing singletons, and many other tracking object creation, implementing singletons, and many other
tasks. tasks.
\index{LBYL}
\item[LBYL]
Look before you leap. This coding style explicitly tests for
pre-conditions before making calls or lookups. This style contrasts
with the \emph{EAFP} approach and is characterized the presence of
many \keyword{if} statements.
\index{mutable} \index{mutable}
\item[mutable] \item[mutable]
Mutable objects can change their value but keep their \function{id()}. Mutable objects can change their value but keep their \function{id()}.
...@@ -280,8 +281,8 @@ See also \emph{immutable}. ...@@ -280,8 +281,8 @@ See also \emph{immutable}.
\index{namespace} \index{namespace}
\item[namespace] \item[namespace]
The place where a variable is stored. Namespaces are implemented as The place where a variable is stored. Namespaces are implemented as
dictionary. There is the local, global and builtins namespace and the dictionaries. There are the local, global and builtin namespaces
nested namespaces in objects (in methods). Namespaces support as well asnested namespaces in objects (in methods). Namespaces support
modularity by preventing naming conflicts. For instance, the modularity by preventing naming conflicts. For instance, the
functions \function{__builtin__.open()} and \function{os.open()} are functions \function{__builtin__.open()} and \function{os.open()} are
distinguished by their namespaces. Namespaces also aid readability distinguished by their namespaces. Namespaces also aid readability
...@@ -312,7 +313,7 @@ classes can use Python's newer, versatile features like ...@@ -312,7 +313,7 @@ classes can use Python's newer, versatile features like
\index{Python3000} \index{Python3000}
\item[Python3000] \item[Python3000]
A mythical python release, allowed not to be backward compatible, with A mythical python release, not required be backward compatible, with
telepathic interface. telepathic interface.
\index{__slots__} \index{__slots__}
...@@ -321,7 +322,7 @@ A declaration inside a \emph{new-style class} that saves memory by ...@@ -321,7 +322,7 @@ A declaration inside a \emph{new-style class} that saves memory by
pre-declaring space for instance attributes and eliminating instance pre-declaring space for instance attributes and eliminating instance
dictionaries. Though popular, the technique is somewhat tricky to get dictionaries. Though popular, the technique is somewhat tricky to get
right and is best reserved for rare cases where there are large right and is best reserved for rare cases where there are large
numbers of instances in a memory critical application. numbers of instances in a memory-critical application.
\index{sequence} \index{sequence}
\item[sequence] \item[sequence]
......
...@@ -2175,7 +2175,7 @@ pattern, list comprehensions can compactly specify the key-value list. ...@@ -2175,7 +2175,7 @@ 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 (2, 4, 6)) # 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}
...@@ -4193,6 +4193,7 @@ in this case (the instance will have a single copy of ``instance ...@@ -4193,6 +4193,7 @@ in this case (the instance will have a single copy of ``instance
variables'' or data attributes used by the common base class), it is variables'' or data attributes used by the common base class), it is
not clear that these semantics are in any way useful. not clear that these semantics are in any way useful.
%% XXX Add rules for new-style MRO?
\section{Private Variables \label{private}} \section{Private Variables \label{private}}
...@@ -4201,9 +4202,9 @@ identifiers. Any identifier of the form \code{__spam} (at least two ...@@ -4201,9 +4202,9 @@ identifiers. Any identifier of the form \code{__spam} (at least two
leading underscores, at most one trailing underscore) is textually leading underscores, at most one trailing underscore) is textually
replaced with \code{_classname__spam}, where \code{classname} is the replaced with \code{_classname__spam}, where \code{classname} is the
current class name with leading underscore(s) stripped. This mangling current class name with leading underscore(s) stripped. This mangling
is done without regard of the syntactic position of the identifier, so is done without regard to the syntactic position of the identifier, so
it can be used to define class-private instance and class variables, it can be used to define class-private instance and class variables,
methods, as well as globals, and even to store instance variables methods, variables stored in globals, and even variables stored in instances.
private to this class on instances of \emph{other} classes. Truncation private to this class on instances of \emph{other} classes. Truncation
may occur when the mangled name would be longer than 255 characters. may occur when the mangled name would be longer than 255 characters.
Outside classes, or when the class name consists of only underscores, Outside classes, or when the class name consists of only underscores,
...@@ -4232,7 +4233,7 @@ when referencing \code{__dict__} directly. ...@@ -4232,7 +4233,7 @@ when referencing \code{__dict__} directly.
\section{Odds and Ends \label{odds}} \section{Odds and Ends \label{odds}}
Sometimes it is useful to have a data type similar to the Pascal Sometimes it is useful to have a data type similar to the Pascal
``record'' or C ``struct'', bundling together a couple of named data ``record'' or C ``struct'', bundling together a few named data
items. An empty class definition will do nicely: items. An empty class definition will do nicely:
\begin{verbatim} \begin{verbatim}
...@@ -4251,7 +4252,7 @@ A piece of Python code that expects a particular abstract data type ...@@ -4251,7 +4252,7 @@ A piece of Python code that expects a particular abstract data type
can often be passed a class that emulates the methods of that data can often be passed a class that emulates the methods of that data
type instead. For instance, if you have a function that formats some type instead. For instance, if you have a function that formats some
data from a file object, you can define a class with methods data from a file object, you can define a class with methods
\method{read()} and \method{readline()} that gets the data from a string \method{read()} and \method{readline()} that get the data from a string
buffer instead, and pass it as an argument.% (Unfortunately, this buffer instead, and pass it as an argument.% (Unfortunately, this
%technique has its limitations: a class can't define operations that %technique has its limitations: a class can't define operations that
%are accessed by special syntax such as sequence subscripting or %are accessed by special syntax such as sequence subscripting or
...@@ -4261,7 +4262,7 @@ buffer instead, and pass it as an argument.% (Unfortunately, this ...@@ -4261,7 +4262,7 @@ buffer instead, and pass it as an argument.% (Unfortunately, this
Instance method objects have attributes, too: \code{m.im_self} is the Instance method objects have attributes, too: \code{m.im_self} is the
object of which the method is an instance, and \code{m.im_func} is the instance object with the method \method{m}, and \code{m.im_func} is the
function object corresponding to the method. function object corresponding to the method.
...@@ -4530,7 +4531,7 @@ wildcard searches: ...@@ -4530,7 +4531,7 @@ wildcard searches:
\section{Command Line Arguments\label{command-line-arguments}} \section{Command Line Arguments\label{command-line-arguments}}
Common utility scripts often invoke processing command line arguments. Common utility scripts often need to process command line arguments.
These arguments are stored in the These arguments are stored in the
\ulink{\module{sys}}{../lib/module-sys.html}\ module's \var{argv} \ulink{\module{sys}}{../lib/module-sys.html}\ module's \var{argv}
attribute as a list. For instance the following output results from attribute as a list. For instance the following output results from
...@@ -4557,7 +4558,7 @@ module also has attributes for \var{stdin}, \var{stdout}, and ...@@ -4557,7 +4558,7 @@ module also has attributes for \var{stdin}, \var{stdout}, and
messages to make them visible even when \var{stdout} has been redirected: messages to make them visible even when \var{stdout} has been redirected:
\begin{verbatim} \begin{verbatim}
>>> sys.stderr.write('Warning, log file not found starting a new one') >>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one Warning, log file not found starting a new one
\end{verbatim} \end{verbatim}
...@@ -4636,7 +4637,7 @@ for sending mail: ...@@ -4636,7 +4637,7 @@ for sending mail:
>>> import smtplib >>> import smtplib
>>> server = smtplib.SMTP('localhost') >>> server = smtplib.SMTP('localhost')
>>> server.sendmail('soothsayer@example.org', 'jceasar@example.org', >>> server.sendmail('soothsayer@example.org', 'jceasar@example.org',
"""To: jceasar@example.org """To: jcaesar@example.org
From: soothsayer@example.org From: soothsayer@example.org
Beware the Ides of March. Beware the Ides of March.
...@@ -4660,8 +4661,8 @@ that are time zone aware. ...@@ -4660,8 +4661,8 @@ that are time zone aware.
>>> now = date.today() >>> now = date.today()
>>> now >>> now
datetime.date(2003, 12, 2) datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y or %d%b %Y is a %A on the %d day of %B") >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03 or 02Dec 2003 is a Tuesday on the 02 day of December' '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
# dates support calendar arithmetic # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31) >>> birthday = date(1964, 7, 31)
...@@ -4691,8 +4692,8 @@ by modules including: ...@@ -4691,8 +4692,8 @@ by modules including:
37 37
>>> zlib.decompress(t) >>> zlib.decompress(t)
'witch which has which witches wrist watch' 'witch which has which witches wrist watch'
>>> zlib.crc32(t) >>> zlib.crc32(s)
-1438085031 226805979
\end{verbatim} \end{verbatim}
...@@ -5219,7 +5220,7 @@ Decimal("0.142857142857142857142857142857142857") ...@@ -5219,7 +5220,7 @@ Decimal("0.142857142857142857142857142857142857")
\chapter{What Now? \label{whatNow}} \chapter{What Now? \label{whatNow}}
Reading this tutorial has probably reinforced your interest in using Reading this tutorial has probably reinforced your interest in using
Python --- you should be eager to apply Python to solve your Python --- you should be eager to apply Python to solving your
real-world problems. Now what should you do? real-world problems. Now what should you do?
You should read, or at least page through, the You should read, or at least page through, the
...@@ -5382,7 +5383,7 @@ A more capable startup file might look like this example. Note that ...@@ -5382,7 +5383,7 @@ A more capable startup file might look like this example. Note that
this deletes the names it creates once they are no longer needed; this this deletes the names it creates once they are no longer needed; this
is done since the startup file is executed in the same namespace as is done since the startup file is executed in the same namespace as
the interactive commands, and removing the names avoids creating side the interactive commands, and removing the names avoids creating side
effects in the interactive environments. You may find it convenient effects in the interactive environment. You may find it convenient
to keep some of the imported modules, such as to keep some of the imported modules, such as
\ulink{\module{os}}{../lib/module-os.html}, which turn \ulink{\module{os}}{../lib/module-os.html}, which turn
out to be needed in most sessions with the interpreter. out to be needed in most sessions with the interpreter.
...@@ -5474,7 +5475,7 @@ or, better, ...@@ -5474,7 +5475,7 @@ or, better,
and so on. No matter how many digits you're willing to write down, the and so on. No matter how many digits you're willing to write down, the
result will never be exactly 1/3, but will be an increasingly better result will never be exactly 1/3, but will be an increasingly better
approximation to 1/3. approximation of 1/3.
In the same way, no matter how many base 2 digits you're willing to In the same way, no matter how many base 2 digits you're willing to
use, the decimal value 0.1 cannot be represented exactly as a base 2 use, the decimal value 0.1 cannot be represented exactly as a base 2
...@@ -5520,7 +5521,7 @@ turns out that's enough (on most machines) so that ...@@ -5520,7 +5521,7 @@ turns out that's enough (on most machines) so that
\var{x}, but rounding to 16 digits is not enough to make that true. \var{x}, but rounding to 16 digits is not enough to make that true.
Note that this is in the very nature of binary floating-point: this is Note that this is in the very nature of binary floating-point: this is
not a bug in Python, it is not a bug in your code either, and you'll not a bug in Python, it is not a bug in your code either. You'll
see the same kind of thing in all languages that support your see the same kind of thing in all languages that support your
hardware's floating-point arithmetic (although some languages may hardware's floating-point arithmetic (although some languages may
not \emph{display} the difference by default, or in all output modes). not \emph{display} the difference by default, or in all output modes).
...@@ -5634,7 +5635,7 @@ and recalling that \var{J} has exactly 53 bits (is \code{>= 2**52} but ...@@ -5634,7 +5635,7 @@ and recalling that \var{J} has exactly 53 bits (is \code{>= 2**52} but
\code{< 2**53}), the best value for \var{N} is 56: \code{< 2**53}), the best value for \var{N} is 56:
\begin{verbatim} \begin{verbatim}
>>> 2L**52 >>> 2**52
4503599627370496L 4503599627370496L
>>> 2L**53 >>> 2L**53
9007199254740992L 9007199254740992L
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment