Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e701dcb

Browse files
committed
SF patch #634866: Alex Martelli's corrections to the ref manual.
Backport candidate. All but one or two of these changes are applicable to 2.2.2.
1 parent 4bad9ba commit e701dcb

3 files changed

Lines changed: 101 additions & 48 deletions

File tree

Doc/ref/ref2.tex

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ \subsection{Reserved classes of identifiers\label{id-classes}}
326326
\lineiii{__*}{Class-private name mangling}{}
327327
\end{tableiii}
328328

329-
(XXX need section references here.)
329+
See sections: \ref{import}, ``The \keyword{import} statement'';
330+
\ref{specialnames}, ``Special method names'';
331+
\ref{atom-identifiers}, ``Identifiers (Names)''.
330332

331333
Note:
332334

@@ -562,18 +564,25 @@ \subsection{Integer and long integer literals\label{integers}}
562564
use \character{L}, since the letter \character{l} looks too much like the
563565
digit \character{1}.
564566

565-
Plain integer decimal literals must be at most 2147483647 (i.e., the
566-
largest positive integer, using 32-bit arithmetic). Plain octal and
567-
hexadecimal literals may be as large as 4294967295, but values larger
568-
than 2147483647 are converted to a negative value by subtracting
569-
4294967296. There is no limit for long integer literals apart from
570-
what can be stored in available memory.
571-
572-
Some examples of plain and long integer literals:
567+
Plain integer decimal literals that are above the largest representable
568+
plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted
569+
as if they were long integers instead. Octal and hexadecimal literals
570+
behave similarly, but when in the range just above the largest representable
571+
plain integer but below the largest unsigned 32-bit number (on a machine
572+
using 32-bit arithmetic), 4294967296, they are taken as the negative plain
573+
integer obtained by subtracting 4294967296 from their unsigned value. There
574+
is no limit for long integer literals apart from what can be stored in
575+
available memory. For example, 0xdeadbeef is taken, on a 32-bit machine,
576+
as the value -559038737, while 0xdeadbeeffeed is taken as the value
577+
244837814107885L.
578+
579+
Some examples of plain integer literals (first row) and long integer
580+
literals (second and third rows):
573581

574582
\begin{verbatim}
575583
7 2147483647 0177 0x80000000
576584
3L 79228162514264337593543950336L 0377L 0x100000000L
585+
79228162514264337593543950336 0xdeadbeeffeed
577586
\end{verbatim}
578587

579588

Doc/ref/ref3.tex

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ \section{Objects, values and types\label{objects}}
1717
\function{id()}\bifuncindex{id} function returns an integer
1818
representing its identity (currently implemented as its address).
1919
An object's \dfn{type} is
20-
also unchangeable. It determines the operations that an object
20+
also unchangeable.\footnote{Since Python 2.2, a gradual merging of
21+
types and classes has been started that makes this and a few other
22+
assertions made in this manual not 100\% accurate and complete:
23+
for example, it \emph{is} now possible in some cases to change an
24+
object's type, under certain controlled conditions. Until this manual
25+
undergoes extensive revision, it must now be taken as authoritative
26+
only regarding ``classic classes'', that are still the default, for
27+
compatibility purposes, in Python 2.2 and 2.3.}
28+
An object's type determines the operations that the object
2129
supports (e.g., ``does it have a length?'') and also defines the
2230
possible values for objects of that type. The
2331
\function{type()}\bifuncindex{type} function returns an object's type
@@ -47,7 +55,7 @@ \section{Objects, values and types\label{objects}}
4755
implemented, as long as no objects are collected that are still
4856
reachable. (Implementation note: the current implementation uses a
4957
reference-counting scheme with (optional) delayed detection of
50-
cyclicly linked garbage, which collects most objects as soon as they
58+
cyclically linked garbage, which collects most objects as soon as they
5159
become unreachable, but is not guaranteed to collect garbage
5260
containing circular references. See the
5361
\citetitle[../lib/module-gc.html]{Python Library Reference} for
@@ -100,7 +108,8 @@ \section{Objects, values and types\label{objects}}
100108
\section{The standard type hierarchy\label{types}}
101109

102110
Below is a list of the types that are built into Python. Extension
103-
modules written in \C{} can define additional types. Future versions of
111+
modules (written in C, Java, or other languages, depending on
112+
the implementation) can define additional types. Future versions of
104113
Python may add types to the type hierarchy (e.g., rational
105114
numbers, efficiently stored arrays of integers, etc.).
106115
\index{type}
@@ -170,7 +179,8 @@ \section{The standard type hierarchy\label{types}}
170179
(The range may be larger on machines with a larger natural word
171180
size, but not smaller.)
172181
When the result of an operation would fall outside this range, the
173-
exception \exception{OverflowError} is raised.
182+
result is normally returned as a long integer (in some cases, the
183+
exception \exception{OverflowError} is raised instead).
174184
For the purpose of shift and mask operations, integers are assumed to
175185
have a binary, 2's complement notation using 32 or more bits, and
176186
hiding no bits from the user (i.e., all 4294967296 different bit
@@ -202,16 +212,16 @@ \section{The standard type hierarchy\label{types}}
202212
The rules for integer representation are intended to give the most
203213
meaningful interpretation of shift and mask operations involving
204214
negative integers and the least surprises when switching between the
205-
plain and long integer domains. For any operation except left shift,
215+
plain and long integer domains. Any operation except left shift,
206216
if it yields a result in the plain integer domain without causing
207-
overflow, it will yield the same result in the long integer domain or
217+
overflow, will yield the same result in the long integer domain or
208218
when using mixed operands.
209219
\indexii{integer}{representation}
210220

211221
\item[Floating point numbers]
212222
These represent machine-level double precision floating point numbers.
213-
You are at the mercy of the underlying machine architecture and
214-
\C{} implementation for the accepted range and handling of overflow.
223+
You are at the mercy of the underlying machine architecture (and
224+
C or Java implementation) for the accepted range and handling of overflow.
215225
Python does not support single-precision floating point numbers; the
216226
savings in processor and memory usage that are usually the reason for using
217227
these is dwarfed by the overhead of using objects in Python, so there
@@ -220,13 +230,14 @@ \section{The standard type hierarchy\label{types}}
220230
\obindex{floating point}
221231
\indexii{floating point}{number}
222232
\indexii{C}{language}
233+
\indexii{Java}{language}
223234

224235
\item[Complex numbers]
225236
These represent complex numbers as a pair of machine-level double
226237
precision floating point numbers. The same caveats apply as for
227-
floating point numbers. The real and imaginary value of a complex
228-
number \code{z} can be retrieved through the attributes \code{z.real}
229-
and \code{z.imag}.
238+
floating point numbers. The real and imaginary parts of a complex
239+
number \code{z} can be retrieved through the read-only attributes
240+
\code{z.real} and \code{z.imag}.
230241
\obindex{complex}
231242
\indexii{complex}{number}
232243

@@ -349,7 +360,7 @@ \section{The standard type hierarchy\label{types}}
349360
\index{subscription}
350361
\index{slicing}
351362

352-
There is currently a single mutable sequence type:
363+
There is currently a single intrinsic mutable sequence type:
353364

354365
\begin{description}
355366

@@ -395,7 +406,7 @@ \section{The standard type hierarchy\label{types}}
395406
\code{1.0}) then they can be used interchangeably to index the same
396407
dictionary entry.
397408

398-
Dictionaries are mutable; they are created by the
409+
Dictionaries are mutable; they can be created by the
399410
\code{\{...\}} notation (see section \ref{dict}, ``Dictionary
400411
Displays'').
401412

@@ -551,10 +562,10 @@ \section{The standard type hierarchy\label{types}}
551562

552563
\item[Built-in methods]
553564
This is really a different disguise of a built-in function, this time
554-
containing an object passed to the \C{} function as an implicit extra
565+
containing an object passed to the C function as an implicit extra
555566
argument. An example of a built-in method is
556-
\code{\var{list}.append()}, assuming
557-
\var{list} is a list object.
567+
\code{\var{alist}.append()}, assuming
568+
\var{alist} is a list object.
558569
In this case, the special read-only attribute \member{__self__} is set
559570
to the object denoted by \var{list}.
560571
\obindex{built-in method}
@@ -940,8 +951,8 @@ \subsection{Basic customization\label{customization}}
940951
\begin{methoddesc}[object]{__init__}{self\optional{, \moreargs}}
941952
Called\indexii{class}{constructor} when the instance is created. The
942953
arguments are those passed to the class constructor expression. If a
943-
base class has an \method{__init__()} method the derived class's
944-
\method{__init__()} method must explicitly call it to ensure proper
954+
base class has an \method{__init__()} method, the derived class's
955+
\method{__init__()} method, if any, must explicitly call it to ensure proper
945956
initialization of the base class part of the instance; for example:
946957
\samp{BaseClass.__init__(\var{self}, [\var{args}...])}. As a special
947958
contraint on constructors, no value may be returned; doing so will
@@ -952,7 +963,8 @@ \subsection{Basic customization\label{customization}}
952963
\begin{methoddesc}[object]{__del__}{self}
953964
Called when the instance is about to be destroyed. This is also
954965
called a destructor\index{destructor}. If a base class
955-
has a \method{__del__()} method, the derived class's \method{__del__()} method
966+
has a \method{__del__()} method, the derived class's \method{__del__()}
967+
method, if any,
956968
must explicitly call it to ensure proper deletion of the base class
957969
part of the instance. Note that it is possible (though not recommended!)
958970
for the \method{__del__()}
@@ -966,9 +978,9 @@ \subsection{Basic customization\label{customization}}
966978
\begin{notice}
967979
\samp{del x} doesn't directly call
968980
\code{x.__del__()} --- the former decrements the reference count for
969-
\code{x} by one, and the latter is only called when its reference
981+
\code{x} by one, and the latter is only called when \code{x}'s reference
970982
count reaches zero. Some common situations that may prevent the
971-
reference count of an object to go to zero include: circular
983+
reference count of an object from going to zero include: circular
972984
references between objects (e.g., a doubly-linked list or a tree data
973985
structure with parent and child pointers); a reference to the object
974986
on the stack frame of a function that caught an exception (the
@@ -1014,6 +1026,9 @@ \subsection{Basic customization\label{customization}}
10141026
this is not possible, a string of the form \samp{<\var{...some useful
10151027
description...}>} should be returned. The return value must be a
10161028
string object.
1029+
If a class defines \method{__repr__()} but not \method{__str__()},
1030+
then \method{__repr__()} is also used when an ``informal'' string
1031+
representation of instances of that class is required.
10171032

10181033
This is typically used for debugging, so it is important that the
10191034
representation is information-rich and unambiguous.
@@ -1053,7 +1068,7 @@ \subsection{Basic customization\label{customization}}
10531068
These methods can return any value, but if the comparison operator is
10541069
used in a Boolean context, the return value should be interpretable as
10551070
a Boolean value, else a \exception{TypeError} will be raised.
1056-
By convention, \code{0} is used for false and \code{1} for true.
1071+
By convention, \code{False} is used for false and \code{True} for true.
10571072

10581073
There are no reflected (swapped-argument) versions of these methods
10591074
(to be used when the left argument does not support the operation but
@@ -1078,7 +1093,7 @@ \subsection{Basic customization\label{customization}}
10781093
support custom comparison operations and are usable as dictionary
10791094
keys.
10801095
(Note: the restriction that exceptions are not propagated by
1081-
\method{__cmp__()} has been removed in Python 1.5.)
1096+
\method{__cmp__()} has been removed since Python 1.5.)
10821097
\bifuncindex{cmp}
10831098
\index{comparisons}
10841099
\end{methoddesc}
@@ -1200,9 +1215,16 @@ \subsection{Emulating container types\label{sequence-types}}
12001215
defined to handle simple, but not extended slices.) It is also recommended
12011216
that mappings provide the methods \method{keys()}, \method{values()},
12021217
\method{items()}, \method{has_key()}, \method{get()}, \method{clear()},
1218+
\method{setdefault()}, \method{iterkeys()}, \method{itervalues()},
1219+
\method{iteritems()}, \method{pop()},, \method{popitem()},
12031220
\method{copy()}, and \method{update()} behaving similar to those for
1204-
Python's standard dictionary objects; mutable sequences should provide
1221+
Python's standard dictionary objects. The \module{UserDict} module
1222+
provides a \class{DictMixin} class to help create those methods
1223+
from a base set of \method{__getitem__()}, \method{__setitem__()},
1224+
\method{__delitem__()}, and \method{keys()}.
1225+
Mutable sequences should provide
12051226
methods \method{append()}, \method{count()}, \method{index()},
1227+
\method{extend()},
12061228
\method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()}
12071229
and \method{sort()}, like Python standard list objects. Finally,
12081230
sequence types should implement addition (meaning concatenation) and
@@ -1214,20 +1236,31 @@ \subsection{Emulating container types\label{sequence-types}}
12141236
implement the \method{__contains__()} method to allow efficient use of
12151237
the \code{in} operator; for mappings, \code{in} should be equivalent
12161238
of \method{has_key()}; for sequences, it should search through the
1217-
values.
1239+
values. It is further recommended that both mappings and sequences
1240+
implement the \method{__iter__()} method to allow efficient iteration
1241+
through the container; for mappings, \method{__iter__()} should be
1242+
the same as \method{iterkeys()}; for sequences, it should iterate
1243+
through the values.
12181244
\withsubitem{(mapping object method)}{
12191245
\ttindex{keys()}
12201246
\ttindex{values()}
12211247
\ttindex{items()}
1248+
\ttindex{iterkeys()}
1249+
\ttindex{itervalues()}
1250+
\ttindex{iteritems()}
12221251
\ttindex{has_key()}
12231252
\ttindex{get()}
1253+
\ttindex{setdefault()}
1254+
\ttindex{pop()}
1255+
\ttindex{popitem()}
12241256
\ttindex{clear()}
12251257
\ttindex{copy()}
12261258
\ttindex{update()}
12271259
\ttindex{__contains__()}}
12281260
\withsubitem{(sequence object method)}{
12291261
\ttindex{append()}
12301262
\ttindex{count()}
1263+
\ttindex{extend()}
12311264
\ttindex{index()}
12321265
\ttindex{insert()}
12331266
\ttindex{pop()}
@@ -1240,7 +1273,8 @@ \subsection{Emulating container types\label{sequence-types}}
12401273
\ttindex{__mul__()}
12411274
\ttindex{__rmul__()}
12421275
\ttindex{__imul__()}
1243-
\ttindex{__contains__()}}
1276+
\ttindex{__contains__()}
1277+
\ttindex{__iter__()}}
12441278
\withsubitem{(numeric object method)}{\ttindex{__coerce__()}}
12451279

12461280
\begin{methoddesc}[container object]{__len__}{self}
@@ -1315,9 +1349,9 @@ \subsection{Emulating container types\label{sequence-types}}
13151349
\subsection{Additional methods for emulation of sequence types
13161350
\label{sequence-methods}}
13171351

1318-
The following methods can be defined to further emulate sequence
1319-
objects. Immutable sequences methods should only define
1320-
\method{__getslice__()}; mutable sequences, should define all three
1352+
The following optional methods can be defined to further emulate sequence
1353+
objects. Immutable sequences methods should at most only define
1354+
\method{__getslice__()}; mutable sequences might define all three
13211355
three methods.
13221356

13231357
\begin{methoddesc}[sequence object]{__getslice__}{self, i, j}
@@ -1341,17 +1375,21 @@ \subsection{Additional methods for emulation of sequence types
13411375
Called to implement assignment to \code{\var{self}[\var{i}:\var{j}]}.
13421376
Same notes for \var{i} and \var{j} as for \method{__getslice__()}.
13431377

1344-
This method is deprecated. If no \method{__setslice__()} is found, a
1345-
slice object is created instead, and passed to \method{__setitem__()}
1346-
instead.
1378+
This method is deprecated. If no \method{__setslice__()} is found,
1379+
or for extended slicing of the form
1380+
\code{\var{self}[\var{i}:\var{j}:\var{k}]}, a
1381+
slice object is created, and passed to \method{__setitem__()},
1382+
instead of \method{__setslice__()} being called.
13471383
\end{methoddesc}
13481384

13491385
\begin{methoddesc}[sequence object]{__delslice__}{self, i, j}
13501386
Called to implement deletion of \code{\var{self}[\var{i}:\var{j}]}.
13511387
Same notes for \var{i} and \var{j} as for \method{__getslice__()}.
1352-
This method is deprecated. If no \method{__delslice__()} is found, a
1353-
slice object is created instead, and passed to \method{__delitem__()}
1354-
instead.
1388+
This method is deprecated. If no \method{__delslice__()} is found,
1389+
or for extended slicing of the form
1390+
\code{\var{self}[\var{i}:\var{j}:\var{k}]}, a
1391+
slice object is created, and passed to \method{__delitem__()},
1392+
instead of \method{__delslice__()} being called.
13551393
\end{methoddesc}
13561394

13571395
Notice that these methods are only invoked when a single slice with a
@@ -1387,8 +1425,8 @@ \subsection{Additional methods for emulation of sequence types
13871425
...
13881426
\end{verbatim}
13891427

1390-
Note the calls to \function{max()}; these are actually necessary due
1391-
to the handling of negative indices before the
1428+
Note the calls to \function{max()}; these are necessary because of
1429+
the handling of negative indices before the
13921430
\method{__*slice__()} methods are called. When negative indexes are
13931431
used, the \method{__*item__()} methods receive them as provided, but
13941432
the \method{__*slice__()} methods get a ``cooked'' form of the index

Doc/ref/ref6.tex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,15 @@ \section{The \keyword{import} statement \label{import}}
638638
\indexii{importing}{module}
639639
\indexii{name}{binding}
640640
\kwindex{from}
641-
% XXX Need to define what ``initialize'' means here
642641
643-
The system maintains a table of modules that have been initialized,
642+
In this context, to ``initialize'' a built-in or extension module means to
643+
call an initialization function that the module must provide for the purpose
644+
(in the reference implementation, the function's name is obtained by
645+
prepending string ``init'' to the module's name); to ``initialize'' a
646+
Python-coded module means to execute the module's body.
647+
648+
The system maintains a table of modules that have been or are being
649+
initialized,
644650
indexed by module name. This table is
645651
accessible as \code{sys.modules}. When a module name is found in
646652
this table, step (1) is finished. If not, a search for a module

0 commit comments

Comments
 (0)