@@ -17,7 +17,15 @@ \section{Objects, values and types\label{objects}}
1717\function {id()}\bifuncindex {id} function returns an integer
1818representing its identity (currently implemented as its address).
1919An 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
2129supports (e.g., `` does it have a length?'' ) and also defines the
2230possible 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}}
4755implemented, as long as no objects are collected that are still
4856reachable. (Implementation note: the current implementation uses a
4957reference-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
5159become unreachable, but is not guaranteed to collect garbage
5260containing 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
102110Below 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
104113Python may add types to the type hierarchy (e.g., rational
105114numbers, 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
171180size, but not smaller.)
172181When 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).
174184For the purpose of shift and mask operations, integers are assumed to
175185have a binary, 2's complement notation using 32 or more bits, and
176186hiding no bits from the user (i.e., all 4294967296 different bit
@@ -202,16 +212,16 @@ \section{The standard type hierarchy\label{types}}
202212The rules for integer representation are intended to give the most
203213meaningful interpretation of shift and mask operations involving
204214negative 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,
206216if 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
208218when using mixed operands.
209219\indexii {integer}{representation}
210220
211221\item [Floating point numbers]
212222These 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.
215225Python does not support single-precision floating point numbers; the
216226savings in processor and memory usage that are usually the reason for using
217227these 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]
225236These represent complex numbers as a pair of machine-level double
226237precision 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
396407dictionary 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
400411Displays'' ).
401412
@@ -551,10 +562,10 @@ \section{The standard type hierarchy\label{types}}
551562
552563\item [Built-in methods]
553564This 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
555566argument. 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.
558569In this case, the special read-only attribute \member {__self__} is set
559570to 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 }}
941952Called\indexii {class}{constructor} when the instance is created. The
942953arguments 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
945956initialization of the base class part of the instance; for example:
946957\samp {BaseClass.__init__(\var {self}, [\var {args}...])}. As a special
947958contraint 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}
953964Called when the instance is about to be destroyed. This is also
954965called 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,
956968must explicitly call it to ensure proper deletion of the base class
957969part of the instance. Note that it is possible (though not recommended!)
958970for 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
970982count 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
972984references between objects (e.g., a doubly-linked list or a tree data
973985structure with parent and child pointers); a reference to the object
974986on the stack frame of a function that caught an exception (the
@@ -1014,6 +1026,9 @@ \subsection{Basic customization\label{customization}}
10141026this is not possible, a string of the form \samp {<\var {...some useful
10151027description...}>} should be returned. The return value must be a
10161028string 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
10181033This is typically used for debugging, so it is important that the
10191034representation is information-rich and unambiguous.
@@ -1053,7 +1068,7 @@ \subsection{Basic customization\label{customization}}
10531068These methods can return any value, but if the comparison operator is
10541069used in a Boolean context, the return value should be interpretable as
10551070a 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
10581073There 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}}
10781093support custom comparison operations and are usable as dictionary
10791094keys.
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}}
12001215defined to handle simple, but not extended slices.) It is also recommended
12011216that 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
12051226methods \method {append()}, \method {count()}, \method {index()},
1227+ \method {extend()},
12061228\method {insert()}, \method {pop()}, \method {remove()}, \method {reverse()}
12071229and \method {sort()}, like Python standard list objects. Finally,
12081230sequence types should implement addition (meaning concatenation) and
@@ -1214,20 +1236,31 @@ \subsection{Emulating container types\label{sequence-types}}
12141236implement the \method {__contains__()} method to allow efficient use of
12151237the \code {in} operator; for mappings, \code {in} should be equivalent
12161238of \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
13211355three methods.
13221356
13231357\begin {methoddesc }[sequence object]{__getslice__}{self, i, j}
@@ -1341,17 +1375,21 @@ \subsection{Additional methods for emulation of sequence types
13411375Called to implement assignment to \code {\var {self}[\var {i}:\var {j}]}.
13421376Same 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}
13501386Called to implement deletion of \code {\var {self}[\var {i}:\var {j}]}.
13511387Same 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
13571395Notice 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
13931431used, the \method {__*item__()} methods receive them as provided, but
13941432the \method {__*slice__()} methods get a `` cooked'' form of the index
0 commit comments