@@ -10,13 +10,6 @@ Built-in Types
1010The following sections describe the standard types that are built into the
1111interpreter.
1212
13- .. note ::
14-
15- Historically (until release 2.2), Python's built-in types have differed from
16- user-defined types because it was not possible to use the built-in types as the
17- basis for object-oriented inheritance. This limitation no longer
18- exists.
19-
2013.. index :: pair: built-in; types
2114
2215The principal built-in types are numerics, sequences, mappings, files, classes,
@@ -129,8 +122,8 @@ Comparisons
129122
130123.. index :: pair: chaining; comparisons
131124
132- Comparison operations are supported by all objects . They all have the same
133- priority (which is higher than that of the Boolean operations). Comparisons can
125+ There are eight comparison operations in Python . They all have the same
126+ priority (which is higher than that of the Boolean operations). Comparisons can
134127be chained arbitrarily; for example, ``x < y <= z `` is equivalent to ``x < y and
135128y <= z ``, except that *y * is evaluated only once (but in both cases *z * is not
136129evaluated at all when ``x < y `` is found to be false).
@@ -172,24 +165,35 @@ This table summarizes the comparison operations:
172165 pair: object; numeric
173166 pair: objects; comparing
174167
175- Objects of different types, except different numeric types and different string
176- types, never compare equal; such objects are ordered consistently but
177- arbitrarily (so that sorting a heterogeneous array yields a consistent result).
168+ Objects of different types, except different numeric types, never compare equal.
178169Furthermore, some types (for example, file objects) support only a degenerate
179- notion of comparison where any two objects of that type are unequal. Again,
180- such objects are ordered arbitrarily but consistently. The ``< ``, ``<= ``, ``> ``
181- and ``>= `` operators will raise a :exc: `TypeError ` exception when any operand is
182- a complex number.
183-
184- .. index :: single: __cmp__() (instance method)
170+ notion of comparison where any two objects of that type are unequal. The ``< ``,
171+ ``<= ``, ``> `` and ``>= `` operators will raise a :exc: `TypeError ` exception when
172+ any operand is a complex number, the objects are of different types that cannot
173+ be compared, or other cases where there is no defined ordering.
174+
175+ .. index ::
176+ single: __cmp__() (instance method)
177+ single: __eq__() (instance method)
178+ single: __ne__() (instance method)
179+ single: __lt__() (instance method)
180+ single: __le__() (instance method)
181+ single: __gt__() (instance method)
182+ single: __ge__() (instance method)
185183
186184Instances of a class normally compare as non-equal unless the class defines the
187- :meth: `__cmp__ ` method. Refer to :ref: `customization `) for information on the
188- use of this method to effect object comparisons.
185+ :meth: `__eq__ ` or :meth: `__cmp__ ` method.
186+
187+ Instances of a class cannot be ordered with respect to other instances of the
188+ same class, or other types of object, unless the class defines enough of the
189+ methods :meth: `__cmp__ `, :meth: `__lt__ `, :meth: `__le__ `, :meth: `__gt__ `, and
190+ :meth: `__ge__ ` (in general, either :meth: `__cmp__ ` or both :meth: `__lt__ ` and
191+ :meth: `__eq__ ` are sufficient, if you want the conventional meanings of the
192+ comparison operators).
189193
190- ** Implementation note: ** Objects of different types except numbers are ordered
191- by their type names; objects of the same types that don't support proper
192- comparison are ordered by their address .
194+ The behavior of the :keyword: ` is ` and :keyword: ` is not ` operators cannot be
195+ customized; also they can be applied to any two objects and never raise an
196+ exception .
193197
194198.. index ::
195199 operator: in
@@ -201,27 +205,22 @@ supported only by sequence types (below).
201205
202206.. _typesnumeric :
203207
204- Numeric Types --- :class: `int `, :class: `float `, :class: `long `, :class: ` complex `
205- ===============================================================================
208+ Numeric Types --- :class: `int `, :class: `float `, :class: `complex `
209+ ================================================================
206210
207211.. index ::
208212 object: numeric
209213 object: Boolean
210214 object: integer
211- object: long integer
212215 object: floating point
213216 object: complex number
214217 pair: C; language
215218
216- There are four distinct numeric types: :dfn: `plain integers `, :dfn: `long
217- integers `, :dfn: `floating point numbers `, and :dfn: `complex numbers `. In
218- addition, Booleans are a subtype of plain integers. Plain integers (also just
219- called :dfn: `integers `) are implemented using :ctype: `long ` in C, which gives
220- them at least 32 bits of precision (``sys.maxint `` is always set to the maximum
221- plain integer value for the current platform, the minimum value is
222- ``-sys.maxint - 1 ``). Long integers have unlimited precision. Floating point
223- numbers are implemented using :ctype: `double ` in C. All bets on their precision
224- are off unless you happen to know the machine you are working with.
219+ There are three distinct numeric types: :dfn: `integers `, :dfn: `floating point
220+ numbers `, and :dfn: `complex numbers `. In addition, Booleans are a subtype of
221+ plain integers. Integers have unlimited precision. loating point numbers are
222+ implemented using :ctype: `double ` in C. All bets on their precision are off
223+ unless you happen to know the machine you are working with.
225224
226225Complex numbers have a real and imaginary part, which are each implemented using
227226:ctype: `double ` in C. To extract these parts from a complex number *z *, use
@@ -230,21 +229,19 @@ Complex numbers have a real and imaginary part, which are each implemented using
230229.. index ::
231230 pair: numeric; literals
232231 pair: integer; literals
233- triple: long; integer; literals
234232 pair: floating point; literals
235233 pair: complex number; literals
236234 pair: hexadecimal; literals
237235 pair: octal; literals
236+ pair: binary: literals
238237
239238Numbers are created by numeric literals or as the result of built-in functions
240- and operators. Unadorned integer literals (including hex and octal numbers)
241- yield plain integers unless the value they denote is too large to be represented
242- as a plain integer, in which case they yield a long integer. Integer literals
243- with an ``'L' `` or ``'l' `` suffix yield long integers (``'L' `` is preferred
244- because ``1l `` looks too much like eleven!). Numeric literals containing a
245- decimal point or an exponent sign yield floating point numbers. Appending
246- ``'j' `` or ``'J' `` to a numeric literal yields a complex number with a zero real
247- part. A complex numeric literal is the sum of a real and an imaginary part.
239+ and operators. Unadorned integer literals (including hex, octal and binary
240+ numbers) yield integers. Numeric literals containing a decimal point or an
241+ exponent sign yield floating point numbers. Appending ``'j' `` or ``'J' `` to a
242+ numeric literal yields an imaginary number (a complex number with a zero real
243+ part) which you can add to an integer or float to get a complex number with real
244+ and imaginary parts.
248245
249246.. index ::
250247 single: arithmetic
@@ -255,58 +252,55 @@ part. A complex numeric literal is the sum of a real and an imaginary part.
255252
256253Python fully supports mixed arithmetic: when a binary arithmetic operator has
257254operands of different numeric types, the operand with the "narrower" type is
258- widened to that of the other, where plain integer is narrower than long integer
259- is narrower than floating point is narrower than complex. Comparisons between
260- numbers of mixed type use the same rule. [# ]_ The constructors :func: `int `,
261- :func: `long `, :func: `float `, and :func: `complex ` can be used to produce numbers
262- of a specific type.
255+ widened to that of the other, where integer is narrower than floating point,
256+ which is narrower than complex. Comparisons between numbers of mixed type use
257+ the same rule. [# ]_ The constructors :func: `int `, :func: `float `, and
258+ :func: `complex ` can be used to produce numbers of a specific type.
263259
264260All numeric types (except complex) support the following operations, sorted by
265261ascending priority (operations in the same box have the same priority; all
266262numeric operations have a higher priority than comparison operations):
267263
268- +--------------------+---------------------------------+--------+
269- | Operation | Result | Notes |
270- +====================+=================================+========+
271- | ``x + y `` | sum of *x * and *y * | |
272- +--------------------+---------------------------------+--------+
273- | ``x - y `` | difference of *x * and *y * | |
274- +--------------------+---------------------------------+--------+
275- | ``x * y `` | product of *x * and *y * | |
276- +--------------------+---------------------------------+--------+
277- | ``x / y `` | quotient of *x * and *y * | \( 1) |
278- +--------------------+---------------------------------+--------+
279- | ``x // y `` | (floored) quotient of *x * and | \( 5) |
280- | | *y * | |
281- +--------------------+---------------------------------+--------+
282- | ``x % y `` | remainder of ``x / y `` | \( 4) |
283- +--------------------+---------------------------------+--------+
284- | ``-x `` | *x * negated | |
285- +--------------------+---------------------------------+--------+
286- | ``+x `` | *x * unchanged | |
287- +--------------------+---------------------------------+--------+
288- | ``abs(x) `` | absolute value or magnitude of | |
289- | | *x * | |
290- +--------------------+---------------------------------+--------+
291- | ``int(x) `` | *x * converted to integer | \( 2) |
292- +--------------------+---------------------------------+--------+
293- | ``long(x) `` | *x * converted to long integer | \( 2) |
294- +--------------------+---------------------------------+--------+
295- | ``float(x) `` | *x * converted to floating point | |
296- +--------------------+---------------------------------+--------+
297- | ``complex(re,im) `` | a complex number with real part | |
298- | | *re *, imaginary part *im *. | |
299- | | *im * defaults to zero. | |
300- +--------------------+---------------------------------+--------+
301- | ``c.conjugate() `` | conjugate of the complex number | |
302- | | *c * | |
303- +--------------------+---------------------------------+--------+
304- | ``divmod(x, y) `` | the pair ``(x // y, x % y) `` | (3)(4) |
305- +--------------------+---------------------------------+--------+
306- | ``pow(x, y) `` | *x * to the power *y * | |
307- +--------------------+---------------------------------+--------+
308- | ``x ** y `` | *x * to the power *y * | |
309- +--------------------+---------------------------------+--------+
264+ +---------------------+---------------------------------+-------+--------------------+
265+ | Operation | Result | Notes | Full documentation |
266+ +==================== +=================================+=======+====================|
267+ | ``x + y `` | sum of *x * and *y * | | |
268+ +---------------------+---------------------------------+-------+--------------------+
269+ | ``x - y `` | difference of *x * and *y * | | |
270+ +---------------------+---------------------------------+-------+--------------------+
271+ | ``x * y `` | product of *x * and *y * | | |
272+ +---------------------+---------------------------------+-------+--------------------+
273+ | ``x / y `` | quotient of *x * and *y * | | |
274+ +---------------------+---------------------------------+-------+--------------------+
275+ | ``x // y `` | floored quotient of *x * and | \( 1) | |
276+ | | *y * | | |
277+ +---------------------+---------------------------------+-------+--------------------+
278+ | ``x % y `` | remainder of ``x / y `` | \( 2) | |
279+ +---------------------+---------------------------------+-------+--------------------+
280+ | ``-x `` | *x * negated | | |
281+ +---------------------+---------------------------------+-------+--------------------+
282+ | ``+x `` | *x * unchanged | | |
283+ +---------------------+---------------------------------+-------+--------------------+
284+ | ``abs(x) `` | absolute value or magnitude of | | :func: `abs ` |
285+ | | *x * | | |
286+ +---------------------+---------------------------------+-------+--------------------+
287+ | ``int(x) `` | *x * converted to integer | \( 3) | :func: `int ` |
288+ +---------------------+---------------------------------+-------+--------------------+
289+ | ``float(x) `` | *x * converted to floating point | | :func: `float ` |
290+ +---------------------+---------------------------------+-------+--------------------+
291+ | ``complex(re, im) `` | a complex number with real part | | :func: `complex ` |
292+ | | *re *, imaginary part *im *. | | |
293+ | | *im * defaults to zero. | | |
294+ +---------------------+---------------------------------+-------+--------------------+
295+ | ``c.conjugate() `` | conjugate of the complex number | | |
296+ | | *c * | | |
297+ +---------------------+---------------------------------+-------+--------------------+
298+ | ``divmod(x, y) `` | the pair ``(x // y, x % y) `` | \( 2) | :func: `divmod ` |
299+ +---------------------+---------------------------------+-------+--------------------+
300+ | ``pow(x, y) `` | *x * to the power *y * | | :func: `pow ` |
301+ +---------------------+---------------------------------+-------+--------------------+
302+ | ``x ** y `` | *x * to the power *y * | | |
303+ +---------------------+---------------------------------+-------+--------------------+
310304
311305.. index ::
312306 triple: operations on; numeric; types
@@ -315,16 +309,16 @@ numeric operations have a higher priority than comparison operations):
315309Notes:
316310
317311(1)
318- .. index ::
319- pair: integer; division
320- triple: long; integer; division
321-
322- For (plain or long) integer division, the result is an integer. The result is
323- always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and
324- (-1)/(-2) is 0. Note that the result is a long integer if either operand is a
325- long integer, regardless of the numeric value.
312+ Also referred to as integer division. The resultant value is a whole
313+ integer, though the result's type is not necessarily int. The result is
314+ always rounded towards minus infinity: ``1//2 `` is ``0 ``, ``(-1)//2 `` is
315+ ``-1 ``, ``1//(-2) `` is ``-1 ``, and ``(-1)//(-2) `` is ``0 ``.
326316
327317(2)
318+ Not for complex numbers. Instead convert to floats using :func: `abs ` if
319+ appropriate.
320+
321+ (3)
328322 .. index ::
329323 module: math
330324 single: floor() (in module math)
@@ -336,19 +330,6 @@ Notes:
336330 as in C; see functions :func: `floor ` and :func: `ceil ` in the :mod: `math ` module
337331 for well-defined conversions.
338332
339- (3)
340- See :ref: `built-in-funcs ` for a full description.
341-
342- (4)
343- Complex floor division operator, modulo operator, and :func: `divmod `.
344-
345- .. deprecated :: 2.3
346- Instead convert to float using :func: `abs ` if appropriate.
347-
348- (5)
349- Also referred to as integer division. The resultant value is a whole integer,
350- though the result's type is not necessarily int.
351-
352333.. % XXXJH exceptions: overflow (when? what operations?) zerodivision
353334
354335
@@ -359,10 +340,9 @@ Bit-string Operations on Integer Types
359340
360341.. _bit-string-operations :
361342
362- Plain and long integer types support additional operations that make sense only
363- for bit-strings. Negative numbers are treated as their 2's complement value
364- (for long integers, this assumes a sufficiently large number of bits that no
365- overflow occurs during the operation).
343+ Integers support additional operations that make sense only for bit-strings.
344+ Negative numbers are treated as their 2's complement value (this assumes a
345+ sufficiently large number of bits that no overflow occurs during the operation).
366346
367347The priorities of the binary bit-wise operations are all lower than the numeric
368348operations and higher than the comparisons; the unary operation ``~ `` has the
@@ -453,7 +433,7 @@ methods, which together form the :dfn:`iterator protocol`:
453433 Python objects in the Python/C API.
454434
455435
456- .. method :: iterator.next ()
436+ .. method :: iterator.__next__ ()
457437
458438 Return the next item from the container. If there are no further items, raise
459439 the :exc: `StopIteration ` exception. This method corresponds to the
@@ -465,11 +445,9 @@ specific sequence types, dictionaries, and other more specialized forms. The
465445specific types are not important beyond their implementation of the iterator
466446protocol.
467447
468- The intention of the protocol is that once an iterator's :meth: `__next__ ` method
469- raises :exc: `StopIteration `, it will continue to do so on subsequent calls.
470- Implementations that do not obey this property are deemed broken. (This
471- constraint was added in Python 2.3; in Python 2.2, various iterators are broken
472- according to this rule.)
448+ Once an iterator's :meth: `__next__ ` method raises :exc: `StopIteration `, it must
449+ continue to do so on subsequent calls. Implementations that do not obey this
450+ property are deemed broken.
473451
474452Python's generators provide a convenient way to implement the iterator protocol.
475453If a container object's :meth: `__iter__ ` method is implemented as a generator,
@@ -1140,13 +1118,9 @@ Notes:
11401118 decimal point and defaults to 6.
11411119
11421120(5)
1143- The ``%r `` conversion was added in Python 2.0.
1144-
11451121 The precision determines the maximal number of characters used.
11461122
11471123
1148- The precision determines the maximal number of characters used.
1149-
11501124Since Python strings have an explicit length, ``%s `` conversions do not assume
11511125that ``'\0' `` is the end of the string.
11521126
@@ -1164,8 +1138,8 @@ Additional string operations are defined in standard modules :mod:`string` and
11641138
11651139.. _typesseq-range :
11661140
1167- XRange Type
1168- -----------
1141+ Range Type
1142+ ----------
11691143
11701144.. index :: object: range
11711145
@@ -1174,7 +1148,7 @@ looping. The advantage of the :class:`range` type is that an :class:`range`
11741148object will always take the same amount of memory, no matter the size of the
11751149range it represents. There are no consistent performance advantages.
11761150
1177- XRange objects have very little behavior: they only support indexing, iteration,
1151+ Range objects have very little behavior: they only support indexing, iteration,
11781152and the :func: `len ` function.
11791153
11801154
0 commit comments