Kaydet (Commit) 71da38b1 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

SF bug #1168135: Python 2.5a0 Tutorial errors and observations (Contributed by Michael R Bax.)

üst e66d4371
...@@ -3924,8 +3924,9 @@ creates a new \emph{instance} of the class and assigns this object to ...@@ -3924,8 +3924,9 @@ creates a new \emph{instance} of the class and assigns this object to
the local variable \code{x}. the local variable \code{x}.
The instantiation operation (``calling'' a class object) creates an The instantiation operation (``calling'' a class object) creates an
empty object. Many classes like to create objects in a known initial empty object. Many classes like to create objects with instances
state. Therefore a class may define a special method named customized to a specific initial state.
Therefore a class may define a special method named
\method{__init__()}, like this: \method{__init__()}, like this:
\begin{verbatim} \begin{verbatim}
...@@ -3981,7 +3982,7 @@ print x.counter ...@@ -3981,7 +3982,7 @@ print x.counter
del x.counter del x.counter
\end{verbatim} \end{verbatim}
The other kind of instance attribute references is a \emph{method}. The other kind of instance attribute reference is a \emph{method}.
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
...@@ -4001,13 +4002,13 @@ a function object. ...@@ -4001,13 +4002,13 @@ a function object.
\subsection{Method Objects \label{methodObjects}} \subsection{Method Objects \label{methodObjects}}
Usually, a method is called immediately: Usually, a method is called right after it is bound:
\begin{verbatim} \begin{verbatim}
x.f() x.f()
\end{verbatim} \end{verbatim}
In our example, this will return the string \code{'hello world'}. In the \class{MyClass} example, this will return the string \code{'hello world'}.
However, it is not necessary to call a method right away: However, it is not necessary to call a method right away:
\code{x.f} is a method object, and can be stored away and called at a \code{x.f} is a method object, and can be stored away and called at a
later time. For example: later time. For example:
...@@ -4085,7 +4086,7 @@ the readability of methods: there is no chance of confusing local ...@@ -4085,7 +4086,7 @@ 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 a method is often called Often, the first argument of a method is 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
...@@ -4149,7 +4150,7 @@ reasons why a method would want to reference its own class! ...@@ -4149,7 +4150,7 @@ reasons why a method would want to reference its own class!
Of course, a language feature would not be worthy of the name ``class'' Of course, a language feature would not be worthy of the name ``class''
without supporting inheritance. The syntax for a derived class without supporting inheritance. The syntax for a derived class
definition looks as follows: definition looks like this:
\begin{verbatim} \begin{verbatim}
class DerivedClassName(BaseClassName): class DerivedClassName(BaseClassName):
...@@ -4161,9 +4162,9 @@ class DerivedClassName(BaseClassName): ...@@ -4161,9 +4162,9 @@ class DerivedClassName(BaseClassName):
\end{verbatim} \end{verbatim}
The name \class{BaseClassName} must be defined in a scope containing The name \class{BaseClassName} must be defined in a scope containing
the derived class definition. Instead of a base class name, an the derived class definition. In place of a base class name, other
expression is also allowed. This is useful when the base class is arbitrary expressions are also allowed. This can be useful, for
defined in another module, example, when the base class is defined in another module:
\begin{verbatim} \begin{verbatim}
class DerivedClassName(modname.BaseClassName): class DerivedClassName(modname.BaseClassName):
...@@ -4172,7 +4173,7 @@ class DerivedClassName(modname.BaseClassName): ...@@ -4172,7 +4173,7 @@ class DerivedClassName(modname.BaseClassName):
Execution of a derived class definition proceeds the same as for a Execution of a derived class definition proceeds the same as for a
base class. When the class object is constructed, the base class is base class. When the class object is constructed, the base class is
remembered. This is used for resolving attribute references: if a remembered. This is used for resolving attribute references: if a
requested attribute is not found in the class, it is searched in the requested attribute is not found in the class, the search proceeds to look in the
base class. This rule is applied recursively if the base class itself base class. This rule is applied recursively if the base class itself
is derived from some other class. is derived from some other class.
...@@ -4185,7 +4186,7 @@ and the method reference is valid if this yields a function object. ...@@ -4185,7 +4186,7 @@ and the method reference is valid if this yields a function object.
Derived classes may override methods of their base classes. Because Derived classes may override methods of their base classes. Because
methods have no special privileges when calling other methods of the methods have no special privileges when calling other methods of the
same object, a method of a base class that calls another method same object, a method of a base class that calls another method
defined in the same base class, may in fact end up calling a method of defined in the same base class may end up calling a method of
a derived class that overrides it. (For \Cpp{} programmers: all methods a derived class that overrides it. (For \Cpp{} programmers: all methods
in Python are effectively \keyword{virtual}.) in Python are effectively \keyword{virtual}.)
...@@ -4200,7 +4201,7 @@ the base class is defined or imported directly in the global scope.) ...@@ -4200,7 +4201,7 @@ the base class is defined or imported directly in the global scope.)
\subsection{Multiple Inheritance \label{multiple}} \subsection{Multiple Inheritance \label{multiple}}
Python supports a limited form of multiple inheritance as well. A Python supports a limited form of multiple inheritance as well. A
class definition with multiple base classes looks as follows: class definition with multiple base classes looks like this:
\begin{verbatim} \begin{verbatim}
class DerivedClassName(Base1, Base2, Base3): class DerivedClassName(Base1, Base2, Base3):
...@@ -4359,15 +4360,15 @@ Note that if the except clauses were reversed (with ...@@ -4359,15 +4360,15 @@ Note that if the except clauses were reversed (with
\samp{except B} first), it would have printed B, B, B --- the first \samp{except B} first), it would have printed B, B, B --- the first
matching except clause is triggered. matching except clause is triggered.
When an error message is printed for an unhandled exception which is a When an error message is printed for an unhandled exception, the
class, the class name is printed, then a colon and a space, and exception's class name is printed, then a colon and a space, and
finally the instance converted to a string using the built-in function finally the instance converted to a string using the built-in function
\function{str()}. \function{str()}.
\section{Iterators\label{iterators}} \section{Iterators\label{iterators}}
By now, you've probably noticed that most container objects can be looped By now you have probably noticed that most container objects can be looped
over using a \keyword{for} statement: over using a \keyword{for} statement:
\begin{verbatim} \begin{verbatim}
...@@ -4406,7 +4407,7 @@ to terminate. This example shows how it all works: ...@@ -4406,7 +4407,7 @@ to terminate. This example shows how it all works:
>>> it.next() >>> it.next()
Traceback (most recent call last): Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel- File "<stdin>", line 1, in ?
it.next() it.next()
StopIteration StopIteration
\end{verbatim} \end{verbatim}
...@@ -4743,7 +4744,7 @@ by modules including: ...@@ -4743,7 +4744,7 @@ by modules including:
\section{Performance Measurement\label{performance-measurement}} \section{Performance Measurement\label{performance-measurement}}
Some Python users develop a deep interest in knowing the relative Some Python users develop a deep interest in knowing the relative
performance between different approaches to the same problem. performance of different approaches to the same problem.
Python provides a measurement tool that answers those questions Python provides a measurement tool that answers those questions
immediately. immediately.
...@@ -4907,7 +4908,7 @@ with group separators: ...@@ -4907,7 +4908,7 @@ with group separators:
>>> locale.format("%d", x, grouping=True) >>> locale.format("%d", x, grouping=True)
'1,234,567' '1,234,567'
>>> locale.format("%s%.*f", (conv['currency_symbol'], >>> locale.format("%s%.*f", (conv['currency_symbol'],
... conv['int_frac_digits'], x), grouping=True) ... conv['frac_digits'], x), grouping=True)
'$1,234,567.80' '$1,234,567.80'
\end{verbatim} \end{verbatim}
...@@ -4974,8 +4975,8 @@ img_1077.jpg --> Ashley_2.jpg ...@@ -4974,8 +4975,8 @@ img_1077.jpg --> Ashley_2.jpg
\end{verbatim} \end{verbatim}
Another application for templating is separating program logic from the Another application for templating is separating program logic from the
details of multiple output formats. The makes it possible to substitute details of multiple output formats. This makes it possible to substitute
custom templates for XML files, plain text reports, and HMTL web reports. custom templates for XML files, plain text reports, and HTML web reports.
\section{Working with Binary Data Record Layouts\label{binary-formats}} \section{Working with Binary Data Record Layouts\label{binary-formats}}
...@@ -4995,7 +4996,7 @@ numbers respectively): ...@@ -4995,7 +4996,7 @@ numbers respectively):
for i in range(3): # show the first 3 file headers for i in range(3): # show the first 3 file headers
start += 14 start += 14
fields = struct.unpack('LLLHH', data[start:start+16]) fields = struct.unpack('LLLHH', data[start:start+16])
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
start += 16 start += 16
filename = data[start:start+filenamesize] filename = data[start:start+filenamesize]
...@@ -5049,7 +5050,7 @@ events, condition variables, and semaphores. ...@@ -5049,7 +5050,7 @@ events, condition variables, and semaphores.
While those tools are powerful, minor design errors can result in While those tools are powerful, minor design errors can result in
problems that are difficult to reproduce. So, the preferred approach problems that are difficult to reproduce. So, the preferred approach
to task coordination is to concentrate all access to a resource to task coordination is to concentrate all access to a resource
in a single thread and then using the in a single thread and then use the
\ulink{\module{Queue}}{../lib/module-Queue.html} module to feed that \ulink{\module{Queue}}{../lib/module-Queue.html} module to feed that
thread with requests from other threads. Applications using thread with requests from other threads. Applications using
\class{Queue} objects for inter-thread communication and coordination \class{Queue} objects for inter-thread communication and coordination
...@@ -5229,7 +5230,7 @@ Decimal("0.7350") ...@@ -5229,7 +5230,7 @@ Decimal("0.7350")
\end{verbatim} \end{verbatim}
The \class{Decimal} result keeps a trailing zero, automatically inferring four The \class{Decimal} result keeps a trailing zero, automatically inferring four
place significance from the two digit multiplicands. Decimal reproduces place significance from multiplicands with two place significance. Decimal reproduces
mathematics as done by hand and avoids issues that can arise when binary mathematics as done by hand and avoids issues that can arise when binary
floating point cannot exactly represent decimal quantities. floating point cannot exactly represent decimal quantities.
......
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