blob: 9073bca763f06aa2e74f399db6c758f374a6e364 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. XXX: reference/datamodel and this have quite a few overlaps!
2
3
4.. _bltin-types:
5
6**************
7Built-in Types
8**************
9
10The following sections describe the standard types that are built into the
11interpreter.
12
Georg Brandl116aa622007-08-15 14:28:22 +000013.. index:: pair: built-in; types
14
15The principal built-in types are numerics, sequences, mappings, files, classes,
16instances and exceptions.
17
Georg Brandl116aa622007-08-15 14:28:22 +000018Some operations are supported by several object types; in particular,
19practically all objects can be compared, tested for truth value, and converted
20to a string (with the :func:`repr` function or the slightly different
21:func:`str` function). The latter function is implicitly used when an object is
22written by the :func:`print` function.
23
24
25.. _truth:
26
27Truth Value Testing
28===================
29
30.. index::
31 statement: if
32 statement: while
33 pair: truth; value
34 pair: Boolean; operations
35 single: false
36
37Any object can be tested for truth value, for use in an :keyword:`if` or
38:keyword:`while` condition or as operand of the Boolean operations below. The
39following values are considered false:
40
41 .. index:: single: None (Built-in object)
42
43* ``None``
44
45 .. index:: single: False (Built-in object)
46
47* ``False``
48
49* zero of any numeric type, for example, ``0``, ``0L``, ``0.0``, ``0j``.
50
51* any empty sequence, for example, ``''``, ``()``, ``[]``.
52
53* any empty mapping, for example, ``{}``.
54
55* instances of user-defined classes, if the class defines a :meth:`__bool__` or
56 :meth:`__len__` method, when that method returns the integer zero or
57 :class:`bool` value ``False``. [#]_
58
59.. index:: single: true
60
61All other values are considered true --- so objects of many types are always
62true.
63
64.. index::
65 operator: or
66 operator: and
67 single: False
68 single: True
69
70Operations and built-in functions that have a Boolean result always return ``0``
71or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
72(Important exception: the Boolean operations ``or`` and ``and`` always return
73one of their operands.)
74
75
76.. _boolean:
77
78Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not`
79====================================================================
80
81.. index:: pair: Boolean; operations
82
83These are the Boolean operations, ordered by ascending priority:
84
85+-------------+---------------------------------+-------+
86| Operation | Result | Notes |
87+=============+=================================+=======+
88| ``x or y`` | if *x* is false, then *y*, else | \(1) |
89| | *x* | |
90+-------------+---------------------------------+-------+
91| ``x and y`` | if *x* is false, then *x*, else | \(2) |
92| | *y* | |
93+-------------+---------------------------------+-------+
94| ``not x`` | if *x* is false, then ``True``, | \(3) |
95| | else ``False`` | |
96+-------------+---------------------------------+-------+
97
98.. index::
99 operator: and
100 operator: or
101 operator: not
102
103Notes:
104
105(1)
106 This is a short-circuit operator, so it only evaluates the second
107 argument if the first one is :const:`False`.
108
109(2)
110 This is a short-circuit operator, so it only evaluates the second
111 argument if the first one is :const:`True`.
112
113(3)
114 ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
115 interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
116
117
118.. _stdcomparisons:
119
120Comparisons
121===========
122
123.. index:: pair: chaining; comparisons
124
Georg Brandl905ec322007-09-28 13:39:25 +0000125There are eight comparison operations in Python. They all have the same
126priority (which is higher than that of the Boolean operations). Comparisons can
Georg Brandl116aa622007-08-15 14:28:22 +0000127be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
128y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
129evaluated at all when ``x < y`` is found to be false).
130
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000131.. index::
132 pair: operator; comparison
133 operator: ==
134 operator: <
135 operator: >
136 operator: <=
137 operator: >=
138 operator: !=
139 operator: is
140 operator: is not
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142This table summarizes the comparison operations:
143
144+------------+-------------------------+-------+
145| Operation | Meaning | Notes |
146+============+=========================+=======+
147| ``<`` | strictly less than | |
148+------------+-------------------------+-------+
149| ``<=`` | less than or equal | |
150+------------+-------------------------+-------+
151| ``>`` | strictly greater than | |
152+------------+-------------------------+-------+
153| ``>=`` | greater than or equal | |
154+------------+-------------------------+-------+
155| ``==`` | equal | |
156+------------+-------------------------+-------+
157| ``!=`` | not equal | |
158+------------+-------------------------+-------+
159| ``is`` | object identity | |
160+------------+-------------------------+-------+
161| ``is not`` | negated object identity | |
162+------------+-------------------------+-------+
163
164.. index::
Georg Brandl116aa622007-08-15 14:28:22 +0000165 pair: object; numeric
166 pair: objects; comparing
167
Georg Brandl905ec322007-09-28 13:39:25 +0000168Objects of different types, except different numeric types, never compare equal.
Georg Brandl116aa622007-08-15 14:28:22 +0000169Furthermore, some types (for example, file objects) support only a degenerate
Georg Brandl905ec322007-09-28 13:39:25 +0000170notion of comparison where any two objects of that type are unequal. The ``<``,
171``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
172any operand is a complex number, the objects are of different types that cannot
173be compared, or other cases where there is no defined ordering.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl905ec322007-09-28 13:39:25 +0000175.. 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)
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184Instances of a class normally compare as non-equal unless the class defines the
Georg Brandl905ec322007-09-28 13:39:25 +0000185:meth:`__eq__` or :meth:`__cmp__` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandl905ec322007-09-28 13:39:25 +0000187Instances of a class cannot be ordered with respect to other instances of the
188same class, or other types of object, unless the class defines enough of the
189methods :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
192comparison operators).
193
194The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
195customized; also they can be applied to any two objects and never raise an
196exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198.. index::
199 operator: in
200 operator: not in
201
202Two more operations with the same syntactic priority, ``in`` and ``not in``, are
203supported only by sequence types (below).
204
205
206.. _typesnumeric:
207
Georg Brandl905ec322007-09-28 13:39:25 +0000208Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
209================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211.. index::
212 object: numeric
213 object: Boolean
214 object: integer
Georg Brandl116aa622007-08-15 14:28:22 +0000215 object: floating point
216 object: complex number
217 pair: C; language
218
Georg Brandl905ec322007-09-28 13:39:25 +0000219There are three distinct numeric types: :dfn:`integers`, :dfn:`floating point
220numbers`, and :dfn:`complex numbers`. In addition, Booleans are a subtype of
Brett Cannon85c1ba52007-10-07 23:12:41 +0000221plain integers. Integers have unlimited precision. Floating point numbers are
Georg Brandl905ec322007-09-28 13:39:25 +0000222implemented using :ctype:`double` in C. All bets on their precision are off
223unless you happen to know the machine you are working with.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225Complex numbers have a real and imaginary part, which are each implemented using
226:ctype:`double` in C. To extract these parts from a complex number *z*, use
227``z.real`` and ``z.imag``.
228
229.. index::
230 pair: numeric; literals
231 pair: integer; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000232 pair: floating point; literals
233 pair: complex number; literals
234 pair: hexadecimal; literals
235 pair: octal; literals
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000236 pair: binary; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238Numbers are created by numeric literals or as the result of built-in functions
Georg Brandl905ec322007-09-28 13:39:25 +0000239and operators. Unadorned integer literals (including hex, octal and binary
240numbers) yield integers. Numeric literals containing a decimal point or an
241exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
242numeric literal yields an imaginary number (a complex number with a zero real
243part) which you can add to an integer or float to get a complex number with real
244and imaginary parts.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246.. index::
247 single: arithmetic
248 builtin: int
249 builtin: long
250 builtin: float
251 builtin: complex
252
253Python fully supports mixed arithmetic: when a binary arithmetic operator has
254operands of different numeric types, the operand with the "narrower" type is
Georg Brandl905ec322007-09-28 13:39:25 +0000255widened to that of the other, where integer is narrower than floating point,
256which is narrower than complex. Comparisons between numbers of mixed type use
257the same rule. [#]_ The constructors :func:`int`, :func:`float`, and
258:func:`complex` can be used to produce numbers of a specific type.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260All numeric types (except complex) support the following operations, sorted by
261ascending priority (operations in the same box have the same priority; all
262numeric operations have a higher priority than comparison operations):
263
Georg Brandl905ec322007-09-28 13:39:25 +0000264+---------------------+---------------------------------+-------+--------------------+
265| Operation | Result | Notes | Full documentation |
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000266+=====================+=================================+=======+====================+
Georg Brandl905ec322007-09-28 13:39:25 +0000267| ``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+---------------------+---------------------------------+-------+--------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305.. index::
306 triple: operations on; numeric; types
307 single: conjugate() (complex number method)
308
309Notes:
310
311(1)
Georg Brandl905ec322007-09-28 13:39:25 +0000312 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``.
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317(2)
Georg Brandl905ec322007-09-28 13:39:25 +0000318 Not for complex numbers. Instead convert to floats using :func:`abs` if
319 appropriate.
320
321(3)
Georg Brandl116aa622007-08-15 14:28:22 +0000322 .. index::
323 module: math
324 single: floor() (in module math)
325 single: ceil() (in module math)
326 pair: numeric; conversions
327 pair: C; language
328
329 Conversion from floating point to (long or plain) integer may round or truncate
330 as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module
331 for well-defined conversions.
332
Georg Brandl116aa622007-08-15 14:28:22 +0000333.. % XXXJH exceptions: overflow (when? what operations?) zerodivision
334
335
336.. _bitstring-ops:
337
338Bit-string Operations on Integer Types
339--------------------------------------
340
341.. _bit-string-operations:
342
Georg Brandl905ec322007-09-28 13:39:25 +0000343Integers support additional operations that make sense only for bit-strings.
344Negative numbers are treated as their 2's complement value (this assumes a
345sufficiently large number of bits that no overflow occurs during the operation).
Georg Brandl116aa622007-08-15 14:28:22 +0000346
347The priorities of the binary bit-wise operations are all lower than the numeric
348operations and higher than the comparisons; the unary operation ``~`` has the
349same priority as the other unary numeric operations (``+`` and ``-``).
350
351This table lists the bit-string operations sorted in ascending priority
352(operations in the same box have the same priority):
353
354+------------+--------------------------------+----------+
355| Operation | Result | Notes |
356+============+================================+==========+
357| ``x | y`` | bitwise :dfn:`or` of *x* and | |
358| | *y* | |
359+------------+--------------------------------+----------+
360| ``x ^ y`` | bitwise :dfn:`exclusive or` of | |
361| | *x* and *y* | |
362+------------+--------------------------------+----------+
363| ``x & y`` | bitwise :dfn:`and` of *x* and | |
364| | *y* | |
365+------------+--------------------------------+----------+
366| ``x << n`` | *x* shifted left by *n* bits | (1), (2) |
367+------------+--------------------------------+----------+
368| ``x >> n`` | *x* shifted right by *n* bits | (1), (3) |
369+------------+--------------------------------+----------+
370| ``~x`` | the bits of *x* inverted | |
371+------------+--------------------------------+----------+
372
373.. index::
374 triple: operations on; integer; types
375 pair: bit-string; operations
376 pair: shifting; operations
377 pair: masking; operations
378
379Notes:
380
381(1)
382 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
383
384(2)
385 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
386 without overflow check.
387
388(3)
389 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
390 overflow check.
391
392
393.. _typeiter:
394
395Iterator Types
396==============
397
Georg Brandl116aa622007-08-15 14:28:22 +0000398.. index::
399 single: iterator protocol
400 single: protocol; iterator
401 single: sequence; iteration
402 single: container; iteration over
403
404Python supports a concept of iteration over containers. This is implemented
405using two distinct methods; these are used to allow user-defined classes to
406support iteration. Sequences, described below in more detail, always support
407the iteration methods.
408
409One method needs to be defined for container objects to provide iteration
410support:
411
412
413.. method:: container.__iter__()
414
415 Return an iterator object. The object is required to support the iterator
416 protocol described below. If a container supports different types of
417 iteration, additional methods can be provided to specifically request
418 iterators for those iteration types. (An example of an object supporting
419 multiple forms of iteration would be a tree structure which supports both
420 breadth-first and depth-first traversal.) This method corresponds to the
421 :attr:`tp_iter` slot of the type structure for Python objects in the Python/C
422 API.
423
424The iterator objects themselves are required to support the following two
425methods, which together form the :dfn:`iterator protocol`:
426
427
428.. method:: iterator.__iter__()
429
430 Return the iterator object itself. This is required to allow both containers
431 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
432 This method corresponds to the :attr:`tp_iter` slot of the type structure for
433 Python objects in the Python/C API.
434
435
Georg Brandl905ec322007-09-28 13:39:25 +0000436.. method:: iterator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438 Return the next item from the container. If there are no further items, raise
439 the :exc:`StopIteration` exception. This method corresponds to the
440 :attr:`tp_iternext` slot of the type structure for Python objects in the
441 Python/C API.
442
443Python defines several iterator objects to support iteration over general and
444specific sequence types, dictionaries, and other more specialized forms. The
445specific types are not important beyond their implementation of the iterator
446protocol.
447
Georg Brandl905ec322007-09-28 13:39:25 +0000448Once an iterator's :meth:`__next__` method raises :exc:`StopIteration`, it must
449continue to do so on subsequent calls. Implementations that do not obey this
450property are deemed broken.
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Georg Brandl9afde1c2007-11-01 20:32:30 +0000452Python's :term:`generator`\s provide a convenient way to implement the iterator
453protocol. If a container object's :meth:`__iter__` method is implemented as a
454generator, it will automatically return an iterator object (technically, a
455generator object) supplying the :meth:`__iter__` and :meth:`__next__` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
458.. _typesseq:
459
Georg Brandl7c676132007-10-23 18:17:00 +0000460Sequence Types --- :class:`str`, :class:`bytes`, :class:`buffer`, :class:`list`, :class:`tuple`, :class:`range`
Georg Brandl4b491312007-08-31 09:22:56 +0000461===============================================================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000462
Georg Brandl7c676132007-10-23 18:17:00 +0000463There are five sequence types: strings, byte sequences, buffers, lists, tuples,
464and range objects. (For other containers see the built-in :class:`dict`,
Georg Brandl4b491312007-08-31 09:22:56 +0000465:class:`list`, :class:`set`, and :class:`tuple` classes, and the
466:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468.. index::
469 object: sequence
470 object: string
Georg Brandl4b491312007-08-31 09:22:56 +0000471 object: bytes
Georg Brandl7c676132007-10-23 18:17:00 +0000472 object: buffer
Georg Brandl116aa622007-08-15 14:28:22 +0000473 object: tuple
474 object: list
Georg Brandl116aa622007-08-15 14:28:22 +0000475 object: range
476
Georg Brandl7c676132007-10-23 18:17:00 +0000477Strings contain Unicode characters. Their literals are written in single or
478double quotes: ``'xyzzy'``, ``"frobozz"``. See :ref:`strings` for more about
479string literals. In addition to the functionality described here, there are
480also string-specific methods described in the :ref:`string-methods` section.
481
482Bytes and buffer objects contain single bytes -- the former is immutable while
483the latter is a mutable sequence. Bytes objects can be constructed from
484literals too; use a ``b`` prefix with normal string syntax: ``b'xyzzy'``.
485To construct buffer objects, use the :func:`buffer` function.
Georg Brandl4b491312007-08-31 09:22:56 +0000486
Georg Brandl226878c2007-08-31 10:15:37 +0000487.. warning::
Georg Brandl4b491312007-08-31 09:22:56 +0000488
489 While string objects are sequences of characters (represented by strings of
Georg Brandl7c676132007-10-23 18:17:00 +0000490 length 1), bytes and buffer objects are sequences of *integers* (between 0
491 and 255), representing the ASCII value of single bytes. That means that for
492 a bytes or buffer object *b*, ``b[0]`` will be an integer, while ``b[0:1]``
493 will be a bytes or buffer object of length 1.
Georg Brandl4b491312007-08-31 09:22:56 +0000494
Georg Brandl2326a792007-09-01 12:08:51 +0000495 Also, while in previous Python versions, byte strings and Unicode strings
496 could be exchanged for each other rather freely (barring encoding issues),
Georg Brandl7c676132007-10-23 18:17:00 +0000497 strings and bytes are now completely separate concepts. There's no implicit
498 en-/decoding if you pass and object of the wrong type. A string always
499 compares unequal to a bytes or buffer object.
Georg Brandl2326a792007-09-01 12:08:51 +0000500
Georg Brandl4b491312007-08-31 09:22:56 +0000501Lists are constructed with square brackets, separating items with commas: ``[a,
502b, c]``. Tuples are constructed by the comma operator (not within square
503brackets), with or without enclosing parentheses, but an empty tuple must have
504the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple
505must have a trailing comma, such as ``(d,)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Georg Brandl4b491312007-08-31 09:22:56 +0000507Objects of type range are similar to buffers in that there is no specific syntax
508to create them, but they are created using the :func:`range` function. They
509don't support slicing, concatenation or repetition, and using ``in``, ``not
510in``, :func:`min` or :func:`max` on them is inefficient.
Georg Brandl116aa622007-08-15 14:28:22 +0000511
512Most sequence types support the following operations. The ``in`` and ``not in``
513operations have the same priorities as the comparison operations. The ``+`` and
514``*`` operations have the same priority as the corresponding numeric operations.
515[#]_
516
517This table lists the sequence operations sorted in ascending priority
518(operations in the same box have the same priority). In the table, *s* and *t*
519are sequences of the same type; *n*, *i* and *j* are integers:
520
521+------------------+--------------------------------+----------+
522| Operation | Result | Notes |
523+==================+================================+==========+
524| ``x in s`` | ``True`` if an item of *s* is | \(1) |
525| | equal to *x*, else ``False`` | |
526+------------------+--------------------------------+----------+
527| ``x not in s`` | ``False`` if an item of *s* is | \(1) |
528| | equal to *x*, else ``True`` | |
529+------------------+--------------------------------+----------+
530| ``s + t`` | the concatenation of *s* and | \(6) |
531| | *t* | |
532+------------------+--------------------------------+----------+
533| ``s * n, n * s`` | *n* shallow copies of *s* | \(2) |
534| | concatenated | |
535+------------------+--------------------------------+----------+
536| ``s[i]`` | *i*'th item of *s*, origin 0 | \(3) |
537+------------------+--------------------------------+----------+
538| ``s[i:j]`` | slice of *s* from *i* to *j* | (3), (4) |
539+------------------+--------------------------------+----------+
540| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3), (5) |
541| | with step *k* | |
542+------------------+--------------------------------+----------+
543| ``len(s)`` | length of *s* | |
544+------------------+--------------------------------+----------+
545| ``min(s)`` | smallest item of *s* | |
546+------------------+--------------------------------+----------+
547| ``max(s)`` | largest item of *s* | |
548+------------------+--------------------------------+----------+
549
Georg Brandl7c676132007-10-23 18:17:00 +0000550Sequence types also support comparisons. In particular, tuples and lists are
551compared lexicographically by comparing corresponding elements. This means that
Georg Brandl4b491312007-08-31 09:22:56 +0000552to compare equal, every element must compare equal and the two sequences must be
Georg Brandl7c676132007-10-23 18:17:00 +0000553of the same type and have the same length. (For full details see
Georg Brandl4b491312007-08-31 09:22:56 +0000554:ref:`comparisons` in the language reference.)
Georg Brandl116aa622007-08-15 14:28:22 +0000555
556.. index::
557 triple: operations on; sequence; types
558 builtin: len
559 builtin: min
560 builtin: max
561 pair: concatenation; operation
562 pair: repetition; operation
563 pair: subscript; operation
564 pair: slice; operation
Georg Brandl116aa622007-08-15 14:28:22 +0000565 operator: in
566 operator: not in
567
568Notes:
569
570(1)
Georg Brandl4b491312007-08-31 09:22:56 +0000571 When *s* is a string object, the ``in`` and ``not in`` operations act like a
572 substring test.
Georg Brandl116aa622007-08-15 14:28:22 +0000573
574(2)
575 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
576 sequence of the same type as *s*). Note also that the copies are shallow;
577 nested structures are not copied. This often haunts new Python programmers;
578 consider::
579
580 >>> lists = [[]] * 3
581 >>> lists
582 [[], [], []]
583 >>> lists[0].append(3)
584 >>> lists
585 [[3], [3], [3]]
586
587 What has happened is that ``[[]]`` is a one-element list containing an empty
Georg Brandl7c676132007-10-23 18:17:00 +0000588 list, so all three elements of ``[[]] * 3`` are (pointers to) this single
589 empty list. Modifying any of the elements of ``lists`` modifies this single
590 list. You can create a list of different lists this way::
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592 >>> lists = [[] for i in range(3)]
593 >>> lists[0].append(3)
594 >>> lists[1].append(5)
595 >>> lists[2].append(7)
596 >>> lists
597 [[3], [5], [7]]
598
599(3)
600 If *i* or *j* is negative, the index is relative to the end of the string:
Georg Brandl7c676132007-10-23 18:17:00 +0000601 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
602 still ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604(4)
605 The slice of *s* from *i* to *j* is defined as the sequence of items with index
606 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
607 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
608 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
609 empty.
610
611(5)
612 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
613 items with index ``x = i + n*k`` such that 0 ≤n < (j-i)/(k). In other words,
614 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
615 *j* is reached (but never including *j*). If *i* or *j* is greater than
616 ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become
617 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
618 If *k* is ``None``, it is treated like ``1``.
619
620(6)
621 If *s* and *t* are both strings, some Python implementations such as CPython can
622 usually perform an in-place optimization for assignments of the form ``s=s+t``
623 or ``s+=t``. When applicable, this optimization makes quadratic run-time much
624 less likely. This optimization is both version and implementation dependent.
625 For performance sensitive code, it is preferable to use the :meth:`str.join`
626 method which assures consistent linear concatenation performance across versions
627 and implementations.
628
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630.. _string-methods:
631
632String Methods
633--------------
634
635.. index:: pair: string; methods
636
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000637String objects support the methods listed below. Note that none of these
638methods take keyword arguments.
639
640In addition, Python's strings support the sequence type methods described in
641the :ref:`typesseq` section. To output formatted strings, see the
642:ref:`string-formatting` section. Also, see the :mod:`re` module for string
643functions based on regular expressions.
Georg Brandl116aa622007-08-15 14:28:22 +0000644
645.. method:: str.capitalize()
646
647 Return a copy of the string with only its first character capitalized.
648
Georg Brandl116aa622007-08-15 14:28:22 +0000649
650.. method:: str.center(width[, fillchar])
651
652 Return centered in a string of length *width*. Padding is done using the
653 specified *fillchar* (default is a space).
654
Georg Brandl116aa622007-08-15 14:28:22 +0000655
656.. method:: str.count(sub[, start[, end]])
657
Georg Brandl9afde1c2007-11-01 20:32:30 +0000658 Return the number of occurrences of substring *sub* in the range [*start*,
659 *end*]. Optional arguments *start* and *end* are interpreted as in slice
660 notation.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
662
Georg Brandl226878c2007-08-31 10:15:37 +0000663.. method:: str.encode([encoding[, errors]])
Georg Brandl116aa622007-08-15 14:28:22 +0000664
665 Return an encoded version of the string. Default encoding is the current
666 default string encoding. *errors* may be given to set a different error
667 handling scheme. The default for *errors* is ``'strict'``, meaning that
668 encoding errors raise a :exc:`UnicodeError`. Other possible values are
669 ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and
670 any other name registered via :func:`codecs.register_error`, see section
671 :ref:`codec-base-classes`. For a list of possible encodings, see section
672 :ref:`standard-encodings`.
673
Georg Brandl116aa622007-08-15 14:28:22 +0000674
675.. method:: str.endswith(suffix[, start[, end]])
676
677 Return ``True`` if the string ends with the specified *suffix*, otherwise return
678 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
679 *start*, test beginning at that position. With optional *end*, stop comparing
680 at that position.
681
Georg Brandl116aa622007-08-15 14:28:22 +0000682
683.. method:: str.expandtabs([tabsize])
684
Georg Brandl9afde1c2007-11-01 20:32:30 +0000685 Return a copy of the string where all tab characters are replaced by one or
686 more spaces, depending on the current column and the given tab size. The
687 column number is reset to zero after each newline occurring in the string.
688 If *tabsize* is not given, a tab size of ``8`` characters is assumed. This
689 doesn't understand other non-printing characters or escape sequences.
Georg Brandl116aa622007-08-15 14:28:22 +0000690
691
692.. method:: str.find(sub[, start[, end]])
693
694 Return the lowest index in the string where substring *sub* is found, such that
695 *sub* is contained in the range [*start*, *end*]. Optional arguments *start*
696 and *end* are interpreted as in slice notation. Return ``-1`` if *sub* is not
697 found.
698
699
Georg Brandl4b491312007-08-31 09:22:56 +0000700.. method:: str.format(format_string, *args, **ksargs)
701
702 Perform a string formatting operation. The *format_string* argument can
703 contain literal text or replacement fields delimited by braces ``{}``. Each
704 replacement field contains either the numeric index of a positional argument,
705 or the name of a keyword argument. Returns a copy of *format_string* where
706 each replacement field is replaced with the string value of the corresponding
707 argument.
708
709 >>> "The sum of 1 + 2 is {0}".format(1+2)
710 'The sum of 1 + 2 is 3'
711
712 See :ref:`formatstrings` for a description of the various formatting options
713 that can be specified in format strings.
714
Georg Brandl4b491312007-08-31 09:22:56 +0000715
Georg Brandl116aa622007-08-15 14:28:22 +0000716.. method:: str.index(sub[, start[, end]])
717
718 Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found.
719
720
721.. method:: str.isalnum()
722
723 Return true if all characters in the string are alphanumeric and there is at
724 least one character, false otherwise.
725
Georg Brandl116aa622007-08-15 14:28:22 +0000726
727.. method:: str.isalpha()
728
729 Return true if all characters in the string are alphabetic and there is at least
730 one character, false otherwise.
731
Georg Brandl116aa622007-08-15 14:28:22 +0000732
733.. method:: str.isdigit()
734
735 Return true if all characters in the string are digits and there is at least one
736 character, false otherwise.
737
Georg Brandl116aa622007-08-15 14:28:22 +0000738
739.. method:: str.isidentifier()
740
741 Return true if the string is a valid identifier according to the language
Georg Brandl4b491312007-08-31 09:22:56 +0000742 definition, section :ref:`identifiers`.
Georg Brandl116aa622007-08-15 14:28:22 +0000743
744
745.. method:: str.islower()
746
747 Return true if all cased characters in the string are lowercase and there is at
748 least one cased character, false otherwise.
749
Georg Brandl116aa622007-08-15 14:28:22 +0000750
751.. method:: str.isspace()
752
753 Return true if there are only whitespace characters in the string and there is
754 at least one character, false otherwise.
755
Georg Brandl116aa622007-08-15 14:28:22 +0000756
757.. method:: str.istitle()
758
759 Return true if the string is a titlecased string and there is at least one
760 character, for example uppercase characters may only follow uncased characters
761 and lowercase characters only cased ones. Return false otherwise.
762
Georg Brandl116aa622007-08-15 14:28:22 +0000763
764.. method:: str.isupper()
765
766 Return true if all cased characters in the string are uppercase and there is at
767 least one cased character, false otherwise.
768
Georg Brandl116aa622007-08-15 14:28:22 +0000769
770.. method:: str.join(seq)
771
Guido van Rossumf1044292007-09-27 18:01:22 +0000772 Return a string which is the concatenation of the values in the sequence
773 *seq*. Non-string values in *seq* will be converted to a string using their
Georg Brandl7c676132007-10-23 18:17:00 +0000774 respective ``str()`` value. If there are any :class:`bytes` objects in
775 *seq*, a :exc:`TypeError` will be raised. The separator between elements is
Guido van Rossumf1044292007-09-27 18:01:22 +0000776 the string providing this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000777
778
779.. method:: str.ljust(width[, fillchar])
780
781 Return the string left justified in a string of length *width*. Padding is done
782 using the specified *fillchar* (default is a space). The original string is
783 returned if *width* is less than ``len(s)``.
784
Georg Brandl116aa622007-08-15 14:28:22 +0000785
786.. method:: str.lower()
787
788 Return a copy of the string converted to lowercase.
789
Georg Brandl116aa622007-08-15 14:28:22 +0000790
791.. method:: str.lstrip([chars])
792
793 Return a copy of the string with leading characters removed. The *chars*
794 argument is a string specifying the set of characters to be removed. If omitted
795 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
796 argument is not a prefix; rather, all combinations of its values are stripped::
797
798 >>> ' spacious '.lstrip()
799 'spacious '
800 >>> 'www.example.com'.lstrip('cmowz.')
801 'example.com'
802
Georg Brandl116aa622007-08-15 14:28:22 +0000803
804.. method:: str.partition(sep)
805
806 Split the string at the first occurrence of *sep*, and return a 3-tuple
807 containing the part before the separator, the separator itself, and the part
808 after the separator. If the separator is not found, return a 3-tuple containing
809 the string itself, followed by two empty strings.
810
Georg Brandl116aa622007-08-15 14:28:22 +0000811
812.. method:: str.replace(old, new[, count])
813
814 Return a copy of the string with all occurrences of substring *old* replaced by
815 *new*. If the optional argument *count* is given, only the first *count*
816 occurrences are replaced.
817
818
Georg Brandl226878c2007-08-31 10:15:37 +0000819.. method:: str.rfind(sub[, start[, end]])
Georg Brandl116aa622007-08-15 14:28:22 +0000820
821 Return the highest index in the string where substring *sub* is found, such that
822 *sub* is contained within s[start,end]. Optional arguments *start* and *end*
823 are interpreted as in slice notation. Return ``-1`` on failure.
824
825
826.. method:: str.rindex(sub[, start[, end]])
827
828 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
829 found.
830
831
832.. method:: str.rjust(width[, fillchar])
833
834 Return the string right justified in a string of length *width*. Padding is done
835 using the specified *fillchar* (default is a space). The original string is
836 returned if *width* is less than ``len(s)``.
837
Georg Brandl116aa622007-08-15 14:28:22 +0000838
839.. method:: str.rpartition(sep)
840
841 Split the string at the last occurrence of *sep*, and return a 3-tuple
842 containing the part before the separator, the separator itself, and the part
843 after the separator. If the separator is not found, return a 3-tuple containing
844 two empty strings, followed by the string itself.
845
Georg Brandl116aa622007-08-15 14:28:22 +0000846
Georg Brandl226878c2007-08-31 10:15:37 +0000847.. method:: str.rsplit([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +0000848
849 Return a list of the words in the string, using *sep* as the delimiter string.
850 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
851 ones. If *sep* is not specified or ``None``, any whitespace string is a
852 separator. Except for splitting from the right, :meth:`rsplit` behaves like
853 :meth:`split` which is described in detail below.
854
Georg Brandl116aa622007-08-15 14:28:22 +0000855
856.. method:: str.rstrip([chars])
857
858 Return a copy of the string with trailing characters removed. The *chars*
859 argument is a string specifying the set of characters to be removed. If omitted
860 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
861 argument is not a suffix; rather, all combinations of its values are stripped::
862
863 >>> ' spacious '.rstrip()
864 ' spacious'
865 >>> 'mississippi'.rstrip('ipz')
866 'mississ'
867
Georg Brandl116aa622007-08-15 14:28:22 +0000868
Georg Brandl226878c2007-08-31 10:15:37 +0000869.. method:: str.split([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +0000870
Georg Brandl226878c2007-08-31 10:15:37 +0000871 Return a list of the words in the string, using *sep* as the delimiter
872 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
873 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
874 specified, then there is no limit on the number of splits (all possible
Georg Brandl9afde1c2007-11-01 20:32:30 +0000875 splits are made).
876
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000877 If *sep* is given, consecutive delimiters are not grouped together and are
Georg Brandl226878c2007-08-31 10:15:37 +0000878 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
879 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
Georg Brandl9afde1c2007-11-01 20:32:30 +0000880 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Georg Brandl226878c2007-08-31 10:15:37 +0000881 Splitting an empty string with a specified separator returns ``['']``.
Georg Brandl116aa622007-08-15 14:28:22 +0000882
883 If *sep* is not specified or is ``None``, a different splitting algorithm is
Georg Brandl9afde1c2007-11-01 20:32:30 +0000884 applied: runs of consecutive whitespace are regarded as a single separator,
885 and the result will contain no empty strings at the start or end if the
886 string has leading or trailing whitespace. Consequently, splitting an empty
887 string or a string consisting of just whitespace with a ``None`` separator
888 returns ``[]``.
889
890 For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and
891 ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
Georg Brandl116aa622007-08-15 14:28:22 +0000892
893
894.. method:: str.splitlines([keepends])
895
896 Return a list of the lines in the string, breaking at line boundaries. Line
897 breaks are not included in the resulting list unless *keepends* is given and
898 true.
899
900
901.. method:: str.startswith(prefix[, start[, end]])
902
903 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
904 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
905 test string beginning at that position. With optional *end*, stop comparing
906 string at that position.
907
Georg Brandl116aa622007-08-15 14:28:22 +0000908
909.. method:: str.strip([chars])
910
911 Return a copy of the string with the leading and trailing characters removed.
912 The *chars* argument is a string specifying the set of characters to be removed.
913 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
914 The *chars* argument is not a prefix or suffix; rather, all combinations of its
915 values are stripped::
916
917 >>> ' spacious '.strip()
918 'spacious'
919 >>> 'www.example.com'.strip('cmowz.')
920 'example'
921
Georg Brandl116aa622007-08-15 14:28:22 +0000922
923.. method:: str.swapcase()
924
925 Return a copy of the string with uppercase characters converted to lowercase and
926 vice versa.
927
Georg Brandl116aa622007-08-15 14:28:22 +0000928
929.. method:: str.title()
930
931 Return a titlecased version of the string: words start with uppercase
932 characters, all remaining cased characters are lowercase.
933
Georg Brandl116aa622007-08-15 14:28:22 +0000934
Georg Brandl4b491312007-08-31 09:22:56 +0000935.. method:: str.translate(map)
Georg Brandl116aa622007-08-15 14:28:22 +0000936
Georg Brandl226878c2007-08-31 10:15:37 +0000937 Return a copy of the *s* where all characters have been mapped through the
Georg Brandlcb8ecb12007-09-04 06:35:14 +0000938 *map* which must be a dictionary of characters (strings of length 1) or
939 Unicode ordinals (integers) to Unicode ordinals, strings or ``None``.
Georg Brandl94c2c752007-10-23 06:52:59 +0000940 Unmapped characters are left untouched. Characters mapped to ``None`` are
Georg Brandlcb8ecb12007-09-04 06:35:14 +0000941 deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000942
Georg Brandl4b491312007-08-31 09:22:56 +0000943 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000944
Georg Brandl4b491312007-08-31 09:22:56 +0000945 A more flexible approach is to create a custom character mapping codec
946 using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
947 example).
Georg Brandl116aa622007-08-15 14:28:22 +0000948
949
950.. method:: str.upper()
951
952 Return a copy of the string converted to uppercase.
953
Georg Brandl116aa622007-08-15 14:28:22 +0000954
955.. method:: str.zfill(width)
956
Georg Brandl9afde1c2007-11-01 20:32:30 +0000957 Return the numeric string left filled with zeros in a string of length
958 *width*. A sign prefix is handled correctly. The original string is
959 returned if *width* is less than ``len(s)``.
960
Georg Brandl116aa622007-08-15 14:28:22 +0000961
Georg Brandl116aa622007-08-15 14:28:22 +0000962
Georg Brandl4b491312007-08-31 09:22:56 +0000963.. _old-string-formatting:
Georg Brandl116aa622007-08-15 14:28:22 +0000964
Georg Brandl4b491312007-08-31 09:22:56 +0000965Old String Formatting Operations
966--------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000967
968.. index::
969 single: formatting, string (%)
970 single: interpolation, string (%)
971 single: string; formatting
972 single: string; interpolation
973 single: printf-style formatting
974 single: sprintf-style formatting
975 single: % formatting
976 single: % interpolation
977
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000978.. XXX is the note enough?
Georg Brandl4b491312007-08-31 09:22:56 +0000979
980.. note::
981
Georg Brandl226878c2007-08-31 10:15:37 +0000982 The formatting operations described here are obsolete and may go away in future
Georg Brandl4b491312007-08-31 09:22:56 +0000983 versions of Python. Use the new :ref:`string-formatting` in new code.
984
985String objects have one unique built-in operation: the ``%`` operator (modulo).
986This is also known as the string *formatting* or *interpolation* operator.
987Given ``format % values`` (where *format* is a string), ``%`` conversion
988specifications in *format* are replaced with zero or more elements of *values*.
989The effect is similar to the using :cfunc:`sprintf` in the C language.
Georg Brandl116aa622007-08-15 14:28:22 +0000990
991If *format* requires a single argument, *values* may be a single non-tuple
992object. [#]_ Otherwise, *values* must be a tuple with exactly the number of
993items specified by the format string, or a single mapping object (for example, a
994dictionary).
995
996A conversion specifier contains two or more characters and has the following
997components, which must occur in this order:
998
999#. The ``'%'`` character, which marks the start of the specifier.
1000
1001#. Mapping key (optional), consisting of a parenthesised sequence of characters
1002 (for example, ``(somename)``).
1003
1004#. Conversion flags (optional), which affect the result of some conversion
1005 types.
1006
1007#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
1008 actual width is read from the next element of the tuple in *values*, and the
1009 object to convert comes after the minimum field width and optional precision.
1010
1011#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
1012 specified as ``'*'`` (an asterisk), the actual width is read from the next
1013 element of the tuple in *values*, and the value to convert comes after the
1014 precision.
1015
1016#. Length modifier (optional).
1017
1018#. Conversion type.
1019
1020When the right argument is a dictionary (or other mapping type), then the
1021formats in the string *must* include a parenthesised mapping key into that
1022dictionary inserted immediately after the ``'%'`` character. The mapping key
1023selects the value to be formatted from the mapping. For example::
1024
Collin Winterc79461b2007-09-01 23:34:30 +00001025 >>> print('%(language)s has %(#)03d quote types.' %
1026 {'language': "Python", "#": 2})
Georg Brandl116aa622007-08-15 14:28:22 +00001027 Python has 002 quote types.
1028
1029In this case no ``*`` specifiers may occur in a format (since they require a
1030sequential parameter list).
1031
1032The conversion flag characters are:
1033
1034+---------+---------------------------------------------------------------------+
1035| Flag | Meaning |
1036+=========+=====================================================================+
1037| ``'#'`` | The value conversion will use the "alternate form" (where defined |
1038| | below). |
1039+---------+---------------------------------------------------------------------+
1040| ``'0'`` | The conversion will be zero padded for numeric values. |
1041+---------+---------------------------------------------------------------------+
1042| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
1043| | conversion if both are given). |
1044+---------+---------------------------------------------------------------------+
1045| ``' '`` | (a space) A blank should be left before a positive number (or empty |
1046| | string) produced by a signed conversion. |
1047+---------+---------------------------------------------------------------------+
1048| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
1049| | (overrides a "space" flag). |
1050+---------+---------------------------------------------------------------------+
1051
1052A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
1053is not necessary for Python.
1054
1055The conversion types are:
1056
1057+------------+-----------------------------------------------------+-------+
1058| Conversion | Meaning | Notes |
1059+============+=====================================================+=======+
1060| ``'d'`` | Signed integer decimal. | |
1061+------------+-----------------------------------------------------+-------+
1062| ``'i'`` | Signed integer decimal. | |
1063+------------+-----------------------------------------------------+-------+
1064| ``'o'`` | Unsigned octal. | \(1) |
1065+------------+-----------------------------------------------------+-------+
1066| ``'u'`` | Unsigned decimal. | |
1067+------------+-----------------------------------------------------+-------+
1068| ``'x'`` | Unsigned hexadecimal (lowercase). | \(2) |
1069+------------+-----------------------------------------------------+-------+
1070| ``'X'`` | Unsigned hexadecimal (uppercase). | \(2) |
1071+------------+-----------------------------------------------------+-------+
1072| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
1073+------------+-----------------------------------------------------+-------+
1074| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
1075+------------+-----------------------------------------------------+-------+
1076| ``'f'`` | Floating point decimal format. | \(3) |
1077+------------+-----------------------------------------------------+-------+
1078| ``'F'`` | Floating point decimal format. | \(3) |
1079+------------+-----------------------------------------------------+-------+
1080| ``'g'`` | Floating point format. Uses exponential format if | \(4) |
1081| | exponent is greater than -4 or less than precision, | |
1082| | decimal format otherwise. | |
1083+------------+-----------------------------------------------------+-------+
1084| ``'G'`` | Floating point format. Uses exponential format if | \(4) |
1085| | exponent is greater than -4 or less than precision, | |
1086| | decimal format otherwise. | |
1087+------------+-----------------------------------------------------+-------+
1088| ``'c'`` | Single character (accepts integer or single | |
1089| | character string). | |
1090+------------+-----------------------------------------------------+-------+
1091| ``'r'`` | String (converts any python object using | \(5) |
1092| | :func:`repr`). | |
1093+------------+-----------------------------------------------------+-------+
Georg Brandl4b491312007-08-31 09:22:56 +00001094| ``'s'`` | String (converts any python object using | |
Georg Brandl116aa622007-08-15 14:28:22 +00001095| | :func:`str`). | |
1096+------------+-----------------------------------------------------+-------+
1097| ``'%'`` | No argument is converted, results in a ``'%'`` | |
1098| | character in the result. | |
1099+------------+-----------------------------------------------------+-------+
1100
1101Notes:
1102
1103(1)
1104 The alternate form causes a leading zero (``'0'``) to be inserted between
1105 left-hand padding and the formatting of the number if the leading character
1106 of the result is not already a zero.
1107
1108(2)
1109 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
1110 the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
1111 and the formatting of the number if the leading character of the result is not
1112 already a zero.
1113
1114(3)
1115 The alternate form causes the result to always contain a decimal point, even if
1116 no digits follow it.
1117
1118 The precision determines the number of digits after the decimal point and
1119 defaults to 6.
1120
1121(4)
1122 The alternate form causes the result to always contain a decimal point, and
1123 trailing zeroes are not removed as they would otherwise be.
1124
1125 The precision determines the number of significant digits before and after the
1126 decimal point and defaults to 6.
1127
1128(5)
Georg Brandl116aa622007-08-15 14:28:22 +00001129 The precision determines the maximal number of characters used.
1130
Georg Brandl116aa622007-08-15 14:28:22 +00001131
Georg Brandl116aa622007-08-15 14:28:22 +00001132Since Python strings have an explicit length, ``%s`` conversions do not assume
1133that ``'\0'`` is the end of the string.
1134
Georg Brandl116aa622007-08-15 14:28:22 +00001135For safety reasons, floating point precisions are clipped to 50; ``%f``
1136conversions for numbers whose absolute value is over 1e25 are replaced by ``%g``
1137conversions. [#]_ All other errors raise exceptions.
1138
1139.. index::
1140 module: string
1141 module: re
1142
1143Additional string operations are defined in standard modules :mod:`string` and
1144:mod:`re`.
1145
1146
1147.. _typesseq-range:
1148
Georg Brandl905ec322007-09-28 13:39:25 +00001149Range Type
1150----------
Georg Brandl116aa622007-08-15 14:28:22 +00001151
1152.. index:: object: range
1153
1154The :class:`range` type is an immutable sequence which is commonly used for
1155looping. The advantage of the :class:`range` type is that an :class:`range`
1156object will always take the same amount of memory, no matter the size of the
1157range it represents. There are no consistent performance advantages.
1158
Georg Brandl905ec322007-09-28 13:39:25 +00001159Range objects have very little behavior: they only support indexing, iteration,
Georg Brandl116aa622007-08-15 14:28:22 +00001160and the :func:`len` function.
1161
1162
1163.. _typesseq-mutable:
1164
1165Mutable Sequence Types
1166----------------------
1167
1168.. index::
1169 triple: mutable; sequence; types
1170 object: list
Georg Brandl7c676132007-10-23 18:17:00 +00001171 object: buffer
Georg Brandl116aa622007-08-15 14:28:22 +00001172
Georg Brandl7c676132007-10-23 18:17:00 +00001173List and buffer objects support additional operations that allow in-place
Georg Brandl226878c2007-08-31 10:15:37 +00001174modification of the object. Other mutable sequence types (when added to the
1175language) should also support these operations. Strings and tuples are
1176immutable sequence types: such objects cannot be modified once created. The
1177following operations are defined on mutable sequence types (where *x* is an
1178arbitrary object).
1179
Georg Brandl7c676132007-10-23 18:17:00 +00001180Note that while lists allow their items to be of any type, buffer object
Georg Brandl226878c2007-08-31 10:15:37 +00001181"items" are all integers in the range 0 <= x < 256.
Georg Brandl116aa622007-08-15 14:28:22 +00001182
1183+------------------------------+--------------------------------+---------------------+
1184| Operation | Result | Notes |
1185+==============================+================================+=====================+
1186| ``s[i] = x`` | item *i* of *s* is replaced by | |
1187| | *x* | |
1188+------------------------------+--------------------------------+---------------------+
1189| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1190| | is replaced by the contents of | |
1191| | the iterable *t* | |
1192+------------------------------+--------------------------------+---------------------+
1193| ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1194+------------------------------+--------------------------------+---------------------+
1195| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1196| | are replaced by those of *t* | |
1197+------------------------------+--------------------------------+---------------------+
1198| ``del s[i:j:k]`` | removes the elements of | |
1199| | ``s[i:j:k]`` from the list | |
1200+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001201| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |
Georg Brandl116aa622007-08-15 14:28:22 +00001202| | [x]`` | |
1203+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001204| ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001205| | x`` | |
1206+------------------------------+--------------------------------+---------------------+
1207| ``s.count(x)`` | return number of *i*'s for | |
1208| | which ``s[i] == x`` | |
1209+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001210| ``s.index(x[, i[, j]])`` | return smallest *k* such that | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001211| | ``s[k] == x`` and ``i <= k < | |
1212| | j`` | |
1213+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001214| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +00001215+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001216| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00001217| | return x`` | |
1218+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001219| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001220+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001221| ``s.reverse()`` | reverses the items of *s* in | \(6) |
Georg Brandl116aa622007-08-15 14:28:22 +00001222| | place | |
1223+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001224| ``s.sort([cmp[, key[, | sort the items of *s* in place | (6), (7) |
Georg Brandl116aa622007-08-15 14:28:22 +00001225| reverse]]])`` | | |
1226+------------------------------+--------------------------------+---------------------+
1227
1228.. index::
1229 triple: operations on; sequence; types
1230 triple: operations on; list; type
1231 pair: subscript; assignment
1232 pair: slice; assignment
Georg Brandl116aa622007-08-15 14:28:22 +00001233 statement: del
Georg Brandl226878c2007-08-31 10:15:37 +00001234 single: append() (sequence method)
1235 single: extend() (sequence method)
1236 single: count() (sequence method)
1237 single: index() (sequence method)
1238 single: insert() (sequence method)
1239 single: pop() (sequence method)
1240 single: remove() (sequence method)
1241 single: reverse() (sequence method)
1242 single: sort() (sequence method)
Georg Brandl116aa622007-08-15 14:28:22 +00001243
1244Notes:
1245
1246(1)
Georg Brandl226878c2007-08-31 10:15:37 +00001247 *t* must have the same length as the slice it is replacing.
Georg Brandl116aa622007-08-15 14:28:22 +00001248
1249(2)
Georg Brandl116aa622007-08-15 14:28:22 +00001250 *x* can be any iterable object.
1251
Georg Brandl226878c2007-08-31 10:15:37 +00001252(3)
Georg Brandl116aa622007-08-15 14:28:22 +00001253 Raises :exc:`ValueError` when *x* is not found in *s*. When a negative index is
Georg Brandl226878c2007-08-31 10:15:37 +00001254 passed as the second or third parameter to the :meth:`index` method, the sequence
Georg Brandl116aa622007-08-15 14:28:22 +00001255 length is added, as for slice indices. If it is still negative, it is truncated
1256 to zero, as for slice indices.
1257
Georg Brandl226878c2007-08-31 10:15:37 +00001258(4)
Georg Brandl116aa622007-08-15 14:28:22 +00001259 When a negative index is passed as the first parameter to the :meth:`insert`
Georg Brandl226878c2007-08-31 10:15:37 +00001260 method, the sequence length is added, as for slice indices. If it is still
Georg Brandl116aa622007-08-15 14:28:22 +00001261 negative, it is truncated to zero, as for slice indices.
1262
Georg Brandl226878c2007-08-31 10:15:37 +00001263(5)
1264 The optional argument *i* defaults to ``-1``, so that by default the last
1265 item is removed and returned.
1266
Georg Brandl116aa622007-08-15 14:28:22 +00001267(6)
Georg Brandl226878c2007-08-31 10:15:37 +00001268 The :meth:`sort` and :meth:`reverse` methods modify the sequence in place for
1269 economy of space when sorting or reversing a large sequence. To remind you
1270 that they operate by side effect, they don't return the sorted or reversed
1271 sequence.
Georg Brandl116aa622007-08-15 14:28:22 +00001272
1273(7)
Georg Brandl7c676132007-10-23 18:17:00 +00001274 :meth:`sort` is not supported by buffer objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001275
Georg Brandl116aa622007-08-15 14:28:22 +00001276 The :meth:`sort` method takes optional arguments for controlling the
1277 comparisons.
1278
1279 *cmp* specifies a custom comparison function of two arguments (list items) which
1280 should return a negative, zero or positive number depending on whether the first
1281 argument is considered smaller than, equal to, or larger than the second
1282 argument: ``cmp=lambda x,y: cmp(x.lower(), y.lower())``
1283
1284 *key* specifies a function of one argument that is used to extract a comparison
1285 key from each list element: ``key=str.lower``
1286
1287 *reverse* is a boolean value. If set to ``True``, then the list elements are
1288 sorted as if each comparison were reversed.
1289
1290 In general, the *key* and *reverse* conversion processes are much faster than
1291 specifying an equivalent *cmp* function. This is because *cmp* is called
1292 multiple times for each list element while *key* and *reverse* touch each
1293 element only once.
1294
Georg Brandl116aa622007-08-15 14:28:22 +00001295 Starting with Python 2.3, the :meth:`sort` method is guaranteed to be stable. A
1296 sort is stable if it guarantees not to change the relative order of elements
1297 that compare equal --- this is helpful for sorting in multiple passes (for
1298 example, sort by department, then by salary grade).
1299
Georg Brandl116aa622007-08-15 14:28:22 +00001300 While a list is being sorted, the effect of attempting to mutate, or even
1301 inspect, the list is undefined. The C implementation of Python 2.3 and newer
1302 makes the list appear empty for the duration, and raises :exc:`ValueError` if it
1303 can detect that the list has been mutated during a sort.
1304
1305
Georg Brandl226878c2007-08-31 10:15:37 +00001306.. _bytes-methods:
1307
Georg Brandl7c676132007-10-23 18:17:00 +00001308Bytes and Buffer Methods
1309------------------------
Georg Brandl226878c2007-08-31 10:15:37 +00001310
1311.. index:: pair: bytes; methods
Georg Brandl7c676132007-10-23 18:17:00 +00001312 pair: buffer; methods
Georg Brandl226878c2007-08-31 10:15:37 +00001313
Georg Brandl7c676132007-10-23 18:17:00 +00001314Bytes and buffer objects, being "strings of bytes", have all methods found on
1315strings, with the exception of :func:`encode`, :func:`format` and
Guido van Rossum98297ee2007-11-06 21:34:58 +00001316:func:`isidentifier`, which do not make sense with these types. For converting
1317the objects to strings, they have a :func:`decode` method.
1318
1319Wherever one of these methods needs to interpret the bytes as characters
1320(e.g. the :func:`is...` methods), the ASCII character set is assumed.
Georg Brandl226878c2007-08-31 10:15:37 +00001321
Georg Brandl7c676132007-10-23 18:17:00 +00001322.. note::
Georg Brandl226878c2007-08-31 10:15:37 +00001323
Georg Brandl7c676132007-10-23 18:17:00 +00001324 The methods on bytes and buffer objects don't accept strings as their
1325 arguments, just as the methods on strings don't accept bytes as their
1326 arguments. For example, you have to write ::
Georg Brandl226878c2007-08-31 10:15:37 +00001327
Georg Brandl7c676132007-10-23 18:17:00 +00001328 a = "abc"
1329 b = a.replace("a", "f")
1330
1331 and ::
1332
1333 a = b"abc"
1334 b = a.replace(b"a", b"f")
Georg Brandl226878c2007-08-31 10:15:37 +00001335
1336
Georg Brandl7c676132007-10-23 18:17:00 +00001337The bytes and buffer types have an additional class method:
Georg Brandl226878c2007-08-31 10:15:37 +00001338
1339.. method:: bytes.fromhex(string)
1340
1341 This :class:`bytes` class method returns a bytes object, decoding the given
1342 string object. The string must contain two hexadecimal digits per byte, spaces
1343 are ignored.
1344
1345 Example::
1346
1347 >>> bytes.fromhex('f0 f1f2 ')
1348 b'\xf0\xf1\xf2'
1349
Georg Brandl7c676132007-10-23 18:17:00 +00001350.. XXX verify/document translate() semantics!
Georg Brandl226878c2007-08-31 10:15:37 +00001351
Georg Brandl7c676132007-10-23 18:17:00 +00001352 .. method:: bytes.translate(table[, delete])
Georg Brandl226878c2007-08-31 10:15:37 +00001353
1354 Return a copy of the bytes object where all bytes occurring in the optional
Georg Brandl7f13e6b2007-08-31 10:37:15 +00001355 argument *delete* are removed, and the remaining bytes have been mapped
Georg Brandl226878c2007-08-31 10:15:37 +00001356 through the given translation table, which must be a bytes object of length
1357 256.
1358
1359 You can use the :func:`maketrans` helper function in the :mod:`string` module to
1360 create a translation table.
1361
1362 .. XXX a None table doesn't seem to be supported
Georg Brandl7f13e6b2007-08-31 10:37:15 +00001363 Set the *table* argument to ``None`` for translations that only delete characters::
Georg Brandl226878c2007-08-31 10:15:37 +00001364
1365 >>> 'read this short text'.translate(None, 'aeiou')
1366 'rd ths shrt txt'
1367
1368
Georg Brandl116aa622007-08-15 14:28:22 +00001369.. _types-set:
1370
1371Set Types --- :class:`set`, :class:`frozenset`
1372==============================================
1373
1374.. index:: object: set
1375
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001376A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001377Common uses include membership testing, removing duplicates from a sequence, and
1378computing mathematical operations such as intersection, union, difference, and
1379symmetric difference.
1380(For other containers see the built in :class:`dict`, :class:`list`,
1381and :class:`tuple` classes, and the :mod:`collections` module.)
1382
Georg Brandl116aa622007-08-15 14:28:22 +00001383Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
1384set``. Being an unordered collection, sets do not record element position or
1385order of insertion. Accordingly, sets do not support indexing, slicing, or
1386other sequence-like behavior.
1387
1388There are currently two builtin set types, :class:`set` and :class:`frozenset`.
1389The :class:`set` type is mutable --- the contents can be changed using methods
1390like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value
1391and cannot be used as either a dictionary key or as an element of another set.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001392The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be
Georg Brandl116aa622007-08-15 14:28:22 +00001393altered after it is created; it can therefore be used as a dictionary key or as
1394an element of another set.
1395
1396The constructors for both classes work the same:
1397
1398.. class:: set([iterable])
1399 frozenset([iterable])
1400
1401 Return a new set or frozenset object whose elements are taken from
1402 *iterable*. The elements of a set must be hashable. To represent sets of
1403 sets, the inner sets must be :class:`frozenset` objects. If *iterable* is
1404 not specified, a new empty set is returned.
1405
1406Instances of :class:`set` and :class:`frozenset` provide the following
1407operations:
1408
1409.. describe:: len(s)
1410
1411 Return the cardinality of set *s*.
1412
1413.. describe:: x in s
1414
1415 Test *x* for membership in *s*.
1416
1417.. describe:: x not in s
1418
1419 Test *x* for non-membership in *s*.
1420
1421.. method:: set.issubset(other)
1422 set <= other
1423
1424 Test whether every element in the set is in *other*.
1425
Georg Brandla6f52782007-09-01 15:49:30 +00001426.. method:: set < other
1427
1428 Test whether the set is a true subset of *other*, that is,
1429 ``set <= other and set != other``.
1430
Georg Brandl116aa622007-08-15 14:28:22 +00001431.. method:: set.issuperset(other)
1432 set >= other
1433
1434 Test whether every element in *other* is in the set.
1435
Georg Brandla6f52782007-09-01 15:49:30 +00001436.. method:: set > other
1437
1438 Test whether the set is a true superset of *other*, that is,
1439 ``set >= other and set != other``.
1440
Georg Brandl116aa622007-08-15 14:28:22 +00001441.. method:: set.union(other)
1442 set | other
1443
1444 Return a new set with elements from both sets.
1445
1446.. method:: set.intersection(other)
1447 set & other
1448
1449 Return a new set with elements common to both sets.
1450
1451.. method:: set.difference(other)
1452 set - other
1453
1454 Return a new set with elements in the set that are not in *other*.
1455
1456.. method:: set.symmetric_difference(other)
1457 set ^ other
1458
1459 Return a new set with elements in either the set or *other* but not both.
1460
1461.. method:: set.copy()
1462
1463 Return a new set with a shallow copy of *s*.
1464
1465
1466Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
1467:meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
1468:meth:`issuperset` methods will accept any iterable as an argument. In
1469contrast, their operator based counterparts require their arguments to be sets.
1470This precludes error-prone constructions like ``set('abc') & 'cbs'`` in favor of
1471the more readable ``set('abc').intersection('cbs')``.
1472
1473Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
1474sets are equal if and only if every element of each set is contained in the
1475other (each is a subset of the other). A set is less than another set if and
1476only if the first set is a proper subset of the second set (is a subset, but is
1477not equal). A set is greater than another set if and only if the first set is a
1478proper superset of the second set (is a superset, but is not equal).
1479
1480Instances of :class:`set` are compared to instances of :class:`frozenset` based
1481on their members. For example, ``set('abc') == frozenset('abc')`` returns
1482``True``.
1483
1484The subset and equality comparisons do not generalize to a complete ordering
1485function. For example, any two disjoint sets are not equal and are not subsets
1486of each other, so *all* of the following return ``False``: ``a<b``, ``a==b``,
1487or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__` method.
1488
1489Since sets only define partial ordering (subset relationships), the output of
1490the :meth:`list.sort` method is undefined for lists of sets.
1491
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001492Set elements, like dictionary keys, must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +00001493
1494Binary operations that mix :class:`set` instances with :class:`frozenset` return
1495the type of the first operand. For example: ``frozenset('ab') | set('bc')``
1496returns an instance of :class:`frozenset`.
1497
1498The following table lists operations available for :class:`set` that do not
1499apply to immutable instances of :class:`frozenset`:
1500
1501.. method:: set.update(other)
1502 set |= other
1503
1504 Update the set, adding elements from *other*.
1505
1506.. method:: set.intersection_update(other)
1507 set &= other
1508
1509 Update the set, keeping only elements found in it and *other*.
1510
1511.. method:: set.difference_update(other)
1512 set -= other
1513
1514 Update the set, removing elements found in *other*.
1515
1516.. method:: set.symmetric_difference_update(other)
1517 set ^= other
1518
1519 Update the set, keeping only elements found in either set, but not in both.
1520
1521.. method:: set.add(el)
1522
1523 Add element *el* to the set.
1524
1525.. method:: set.remove(el)
1526
1527 Remove element *el* from the set. Raises :exc:`KeyError` if *el* is not
1528 contained in the set.
1529
1530.. method:: set.discard(el)
1531
1532 Remove element *el* from the set if it is present.
1533
1534.. method:: set.pop()
1535
1536 Remove and return an arbitrary element from the set. Raises :exc:`KeyError`
1537 if the set is empty.
1538
1539.. method:: set.clear()
1540
1541 Remove all elements from the set.
1542
1543
1544Note, the non-operator versions of the :meth:`update`,
1545:meth:`intersection_update`, :meth:`difference_update`, and
1546:meth:`symmetric_difference_update` methods will accept any iterable as an
1547argument.
1548
1549
1550.. _typesmapping:
1551
1552Mapping Types --- :class:`dict`
1553===============================
1554
1555.. index::
1556 object: mapping
1557 object: dictionary
1558 triple: operations on; mapping; types
1559 triple: operations on; dictionary; type
1560 statement: del
1561 builtin: len
1562
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001563A :dfn:`mapping` object maps :term:`hashable` values to arbitrary objects.
1564Mappings are mutable objects. There is currently only one standard mapping
1565type, the :dfn:`dictionary`. (For other containers see the built in
1566:class:`list`, :class:`set`, and :class:`tuple` classes, and the
1567:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +00001568
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001569A dictionary's keys are *almost* arbitrary values. Values that are not
1570:term:`hashable`, that is, values containing lists, dictionaries or other
1571mutable types (that are compared by value rather than by object identity) may
1572not be used as keys. Numeric types used for keys obey the normal rules for
1573numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
1574then they can be used interchangeably to index the same dictionary entry. (Note
1575however, that since computers store floating-point numbers as approximations it
1576is usually unwise to use them as dictionary keys.)
Georg Brandl116aa622007-08-15 14:28:22 +00001577
1578Dictionaries can be created by placing a comma-separated list of ``key: value``
1579pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
1580'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
1581
1582.. class:: dict([arg])
1583
Georg Brandld22a8152007-09-04 17:43:37 +00001584 Return a new dictionary initialized from an optional positional argument or
1585 from a set of keyword arguments. If no arguments are given, return a new
1586 empty dictionary. If the positional argument *arg* is a mapping object,
1587 return a dictionary mapping the same keys to the same values as does the
1588 mapping object. Otherwise the positional argument must be a sequence, a
1589 container that supports iteration, or an iterator object. The elements of
1590 the argument must each also be of one of those kinds, and each must in turn
1591 contain exactly two objects. The first is used as a key in the new
1592 dictionary, and the second as the key's value. If a given key is seen more
1593 than once, the last value associated with it is retained in the new
1594 dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00001595
1596 If keyword arguments are given, the keywords themselves with their associated
Georg Brandld22a8152007-09-04 17:43:37 +00001597 values are added as items to the dictionary. If a key is specified both in
1598 the positional argument and as a keyword argument, the value associated with
1599 the keyword is retained in the dictionary. For example, these all return a
Georg Brandl116aa622007-08-15 14:28:22 +00001600 dictionary equal to ``{"one": 2, "two": 3}``:
1601
1602 * ``dict(one=2, two=3)``
Georg Brandl116aa622007-08-15 14:28:22 +00001603 * ``dict({'one': 2, 'two': 3})``
Georg Brandl116aa622007-08-15 14:28:22 +00001604 * ``dict(zip(('one', 'two'), (2, 3)))``
Georg Brandl116aa622007-08-15 14:28:22 +00001605 * ``dict([['two', 3], ['one', 2]])``
1606
Georg Brandld22a8152007-09-04 17:43:37 +00001607 The first example only works for keys that are valid Python identifiers; the
1608 others work with any valid keys.
Georg Brandl116aa622007-08-15 14:28:22 +00001609
Georg Brandl116aa622007-08-15 14:28:22 +00001610
1611These are the operations that dictionaries support (and therefore, custom mapping
1612types should support too):
1613
1614.. describe:: len(d)
1615
1616 Return the number of items in the dictionary *d*.
1617
1618.. describe:: d[key]
1619
1620 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
1621 not in the map.
1622
Georg Brandl55ac8f02007-09-01 13:51:09 +00001623 If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
1624 is not present, the ``d[key]`` operation calls that method with the key *key*
1625 as argument. The ``d[key]`` operation then returns or raises whatever is
1626 returned or raised by the ``__missing__(key)`` call if the key is not
1627 present. No other operations or methods invoke :meth:`__missing__`. If
1628 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
1629 :meth:`__missing__` must be a method; it cannot be an instance variable. For
1630 an example, see :class:`collections.defaultdict`.
Georg Brandl116aa622007-08-15 14:28:22 +00001631
1632.. describe:: d[key] = value
1633
1634 Set ``d[key]`` to *value*.
1635
1636.. describe:: del d[key]
1637
1638 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
1639 map.
1640
1641.. describe:: key in d
1642
1643 Return ``True`` if *d* has a key *key*, else ``False``.
1644
Georg Brandl116aa622007-08-15 14:28:22 +00001645.. describe:: key not in d
1646
1647 Equivalent to ``not key in d``.
1648
Georg Brandl116aa622007-08-15 14:28:22 +00001649.. method:: dict.clear()
1650
1651 Remove all items from the dictionary.
1652
1653.. method:: dict.copy()
1654
1655 Return a shallow copy of the dictionary.
1656
1657.. method:: dict.fromkeys(seq[, value])
1658
1659 Create a new dictionary with keys from *seq* and values set to *value*.
1660
1661 :func:`fromkeys` is a class method that returns a new dictionary. *value*
1662 defaults to ``None``.
1663
Georg Brandl116aa622007-08-15 14:28:22 +00001664.. method:: dict.get(key[, default])
1665
1666 Return the value for *key* if *key* is in the dictionary, else *default*. If
1667 *default* is not given, it defaults to ``None``, so that this method never
1668 raises a :exc:`KeyError`.
1669
Georg Brandl116aa622007-08-15 14:28:22 +00001670.. method:: dict.items()
1671
Georg Brandld22a8152007-09-04 17:43:37 +00001672 Return a new view of the dictionary's items (``(key, value)`` pairs). See
1673 below for documentation of view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001674
Georg Brandl116aa622007-08-15 14:28:22 +00001675.. method:: dict.keys()
1676
Georg Brandld22a8152007-09-04 17:43:37 +00001677 Return a new view of the dictionary's keys. See below for documentation of
1678 view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001679
1680.. method:: dict.pop(key[, default])
1681
1682 If *key* is in the dictionary, remove it and return its value, else return
1683 *default*. If *default* is not given and *key* is not in the dictionary, a
1684 :exc:`KeyError` is raised.
1685
Georg Brandl116aa622007-08-15 14:28:22 +00001686.. method:: dict.popitem()
1687
1688 Remove and return an arbitrary ``(key, value)`` pair from the dictionary.
1689
1690 :func:`popitem` is useful to destructively iterate over a dictionary, as
1691 often used in set algorithms. If the dictionary is empty, calling
1692 :func:`popitem` raises a :exc:`KeyError`.
1693
1694.. method:: dict.setdefault(key[, default])
1695
Fred Drake2e748782007-09-04 17:33:11 +00001696 If *key* is in the dictionary, return its value. If not, insert *key* with
1697 a value of *default* and return *default*. *default* defaults to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001698
1699.. method:: dict.update([other])
1700
Fred Drake2e748782007-09-04 17:33:11 +00001701 Update the dictionary with the key/value pairs from *other*, overwriting
1702 existing keys. Return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001703
1704 :func:`update` accepts either another dictionary object or an iterable of
1705 key/value pairs (as a tuple or other iterable of length two). If keyword
1706 arguments are specified, the dictionary is then is updated with those
1707 key/value pairs: ``d.update(red=1, blue=2)``.
1708
Georg Brandl116aa622007-08-15 14:28:22 +00001709.. method:: dict.values()
1710
Georg Brandld22a8152007-09-04 17:43:37 +00001711 Return a new view of the dictionary's values. See below for documentation of
1712 view objects.
1713
1714
1715Dictionary view objects
1716-----------------------
1717
1718The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
1719:meth:`dict.items` are *view objects*. They provide a dynamic view on the
1720dictionary's entries, which means that when the dictionary changes, the view
1721reflects these changes. The keys and items views have a set-like character
1722since their entries
1723
1724Dictionary views can be iterated over to yield their respective data, and
1725support membership tests:
1726
1727.. describe:: len(dictview)
1728
1729 Return the number of entries in the dictionary.
1730
1731.. describe:: iter(dictview)
1732
1733 Return an iterator over the keys, values or items (represented as tuples of
1734 ``(key, value)``) in the dictionary.
1735
1736 Keys and values are iterated over in an arbitrary order which is non-random,
1737 varies across Python implementations, and depends on the dictionary's history
1738 of insertions and deletions. If keys, values and items views are iterated
1739 over with no intervening modifications to the dictionary, the order of items
1740 will directly correspond. This allows the creation of ``(value, key)`` pairs
1741 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
1742 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
1743
1744.. describe:: x in dictview
1745
1746 Return ``True`` if *x* is in the underlying dictionary's keys, values or
1747 items (in the latter case, *x* should be a ``(key, value)`` tuple).
1748
1749
1750The keys and items views also provide set-like operations ("other" here refers
1751to another dictionary view or a set):
1752
1753.. describe:: dictview & other
1754
1755 Return the intersection of the dictview and the other object as a new set.
1756
1757.. describe:: dictview | other
1758
1759 Return the union of the dictview and the other object as a new set.
1760
1761.. describe:: dictview - other
1762
1763 Return the difference between the dictview and the other object (all elements
1764 in *dictview* that aren't in *other*) as a new set.
1765
1766.. describe:: dictview ^ other
1767
1768 Return the symmetric difference (all elements either in *dictview* or
1769 *other*, but not in both) of the dictview and the other object as a new set.
1770
1771.. warning::
1772
1773 Since a dictionary's values are not required to be hashable, any of these
1774 four operations will fail if an involved dictionary contains such a value.
Georg Brandl116aa622007-08-15 14:28:22 +00001775
1776
Georg Brandlc53c9662007-09-04 17:58:02 +00001777An example of dictionary view usage::
1778
1779 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
1780 >>> keys = dishes.keys()
1781 >>> values = dishes.values()
1782
1783 >>> # iteration
1784 >>> n = 0
1785 >>> for val in values:
1786 ... n += val
1787 >>> print(n)
1788 504
1789
1790 >>> # keys and values are iterated over in the same order
1791 >>> list(keys)
1792 ['eggs', 'bacon', 'sausage', 'spam']
1793 >>> list(values)
1794 [2, 1, 1, 500]
1795
1796 >>> # view objects are dynamic and reflect dict changes
1797 >>> del dishes['eggs']
1798 >>> del dishes['sausage']
1799 >>> list(keys)
1800 ['spam', 'bacon']
1801
1802 >>> # set operations
1803 >>> keys & {'eggs', 'bacon', 'salad'}
1804 {'eggs', 'bacon'}
1805
1806
Georg Brandl116aa622007-08-15 14:28:22 +00001807.. _bltin-file-objects:
1808
1809File Objects
1810============
1811
1812.. index::
1813 object: file
1814 builtin: file
1815 module: os
1816 module: socket
1817
Georg Brandl81ac1ce2007-08-31 17:17:17 +00001818.. XXX this is quite out of date, must be updated with "io" module
1819
Georg Brandl116aa622007-08-15 14:28:22 +00001820File objects are implemented using C's ``stdio`` package and can be
1821created with the built-in :func:`file` and (more usually) :func:`open`
1822constructors described in the :ref:`built-in-funcs` section. [#]_ File
1823objects are also returned by some other built-in functions and methods,
1824such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile`
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001825method of socket objects. Temporary files can be created using the
1826:mod:`tempfile` module, and high-level file operations such as copying,
1827moving, and deleting files and directories can be achieved with the
1828:mod:`shutil` module.
Georg Brandl116aa622007-08-15 14:28:22 +00001829
1830When a file operation fails for an I/O-related reason, the exception
1831:exc:`IOError` is raised. This includes situations where the operation is not
1832defined for some reason, like :meth:`seek` on a tty device or writing a file
1833opened for reading.
1834
1835Files have the following methods:
1836
1837
1838.. method:: file.close()
1839
1840 Close the file. A closed file cannot be read or written any more. Any operation
1841 which requires that the file be open will raise a :exc:`ValueError` after the
1842 file has been closed. Calling :meth:`close` more than once is allowed.
1843
1844 As of Python 2.5, you can avoid having to call this method explicitly if you use
1845 the :keyword:`with` statement. For example, the following code will
1846 automatically close ``f`` when the :keyword:`with` block is exited::
1847
1848 from __future__ import with_statement
1849
1850 with open("hello.txt") as f:
1851 for line in f:
Collin Winterc79461b2007-09-01 23:34:30 +00001852 print(line)
Georg Brandl116aa622007-08-15 14:28:22 +00001853
1854 In older versions of Python, you would have needed to do this to get the same
1855 effect::
1856
1857 f = open("hello.txt")
1858 try:
1859 for line in f:
Collin Winterc79461b2007-09-01 23:34:30 +00001860 print(line)
Georg Brandl116aa622007-08-15 14:28:22 +00001861 finally:
1862 f.close()
1863
1864 .. note::
1865
1866 Not all "file-like" types in Python support use as a context manager for the
1867 :keyword:`with` statement. If your code is intended to work with any file-like
1868 object, you can use the function :func:`contextlib.closing` instead of using
1869 the object directly.
1870
1871
1872.. method:: file.flush()
1873
1874 Flush the internal buffer, like ``stdio``'s :cfunc:`fflush`. This may be a
1875 no-op on some file-like objects.
1876
1877
1878.. method:: file.fileno()
1879
1880 .. index::
Georg Brandl9afde1c2007-11-01 20:32:30 +00001881 pair: file; descriptor
Georg Brandl116aa622007-08-15 14:28:22 +00001882 module: fcntl
1883
1884 Return the integer "file descriptor" that is used by the underlying
1885 implementation to request I/O operations from the operating system. This can be
1886 useful for other, lower level interfaces that use file descriptors, such as the
1887 :mod:`fcntl` module or :func:`os.read` and friends.
1888
1889 .. note::
1890
1891 File-like objects which do not have a real file descriptor should *not* provide
1892 this method!
1893
1894
1895.. method:: file.isatty()
1896
1897 Return ``True`` if the file is connected to a tty(-like) device, else ``False``.
1898
1899 .. note::
1900
1901 If a file-like object is not associated with a real file, this method should
1902 *not* be implemented.
1903
1904
1905.. method:: file.__next__()
1906
1907 A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
1908 *f* is closed). When a file is used as an iterator, typically in a
Georg Brandl6911e3c2007-09-04 07:15:32 +00001909 :keyword:`for` loop (for example, ``for line in f: print(line)``), the
Georg Brandl116aa622007-08-15 14:28:22 +00001910 :meth:`__next__` method is called repeatedly. This method returns the next
1911 input line, or raises :exc:`StopIteration` when EOF is hit when the file is open
1912 for reading (behavior is undefined when the file is open for writing). In order
1913 to make a :keyword:`for` loop the most efficient way of looping over the lines
1914 of a file (a very common operation), the :meth:`__next__` method uses a hidden
1915 read-ahead buffer. As a consequence of using a read-ahead buffer, combining
1916 :meth:`__next__` with other file methods (like :meth:`readline`) does not work
1917 right. However, using :meth:`seek` to reposition the file to an absolute
1918 position will flush the read-ahead buffer.
1919
Georg Brandl116aa622007-08-15 14:28:22 +00001920
1921.. method:: file.read([size])
1922
1923 Read at most *size* bytes from the file (less if the read hits EOF before
1924 obtaining *size* bytes). If the *size* argument is negative or omitted, read
1925 all data until EOF is reached. The bytes are returned as a string object. An
1926 empty string is returned when EOF is encountered immediately. (For certain
1927 files, like ttys, it makes sense to continue reading after an EOF is hit.) Note
1928 that this method may call the underlying C function :cfunc:`fread` more than
1929 once in an effort to acquire as close to *size* bytes as possible. Also note
1930 that when in non-blocking mode, less data than what was requested may be
1931 returned, even if no *size* parameter was given.
1932
1933
1934.. method:: file.readline([size])
1935
1936 Read one entire line from the file. A trailing newline character is kept in the
1937 string (but may be absent when a file ends with an incomplete line). [#]_ If
1938 the *size* argument is present and non-negative, it is a maximum byte count
1939 (including the trailing newline) and an incomplete line may be returned. An
1940 empty string is returned *only* when EOF is encountered immediately.
1941
1942 .. note::
1943
1944 Unlike ``stdio``'s :cfunc:`fgets`, the returned string contains null characters
1945 (``'\0'``) if they occurred in the input.
1946
1947
1948.. method:: file.readlines([sizehint])
1949
1950 Read until EOF using :meth:`readline` and return a list containing the lines
1951 thus read. If the optional *sizehint* argument is present, instead of
1952 reading up to EOF, whole lines totalling approximately *sizehint* bytes
1953 (possibly after rounding up to an internal buffer size) are read. Objects
1954 implementing a file-like interface may choose to ignore *sizehint* if it
1955 cannot be implemented, or cannot be implemented efficiently.
1956
1957
1958.. method:: file.seek(offset[, whence])
1959
1960 Set the file's current position, like ``stdio``'s :cfunc:`fseek`. The *whence*
1961 argument is optional and defaults to ``os.SEEK_SET`` or ``0`` (absolute file
1962 positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the
1963 current position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's
1964 end). There is no return value. Note that if the file is opened for appending
1965 (mode ``'a'`` or ``'a+'``), any :meth:`seek` operations will be undone at the
1966 next write. If the file is only opened for writing in append mode (mode
1967 ``'a'``), this method is essentially a no-op, but it remains useful for files
1968 opened in append mode with reading enabled (mode ``'a+'``). If the file is
1969 opened in text mode (without ``'b'``), only offsets returned by :meth:`tell` are
1970 legal. Use of other offsets causes undefined behavior.
1971
1972 Note that not all file objects are seekable.
1973
Georg Brandl116aa622007-08-15 14:28:22 +00001974
1975.. method:: file.tell()
1976
1977 Return the file's current position, like ``stdio``'s :cfunc:`ftell`.
1978
1979 .. note::
1980
1981 On Windows, :meth:`tell` can return illegal values (after an :cfunc:`fgets`)
1982 when reading files with Unix-style line-endings. Use binary mode (``'rb'``) to
1983 circumvent this problem.
1984
1985
1986.. method:: file.truncate([size])
1987
1988 Truncate the file's size. If the optional *size* argument is present, the file
1989 is truncated to (at most) that size. The size defaults to the current position.
1990 The current file position is not changed. Note that if a specified size exceeds
1991 the file's current size, the result is platform-dependent: possibilities
1992 include that the file may remain unchanged, increase to the specified size as if
1993 zero-filled, or increase to the specified size with undefined new content.
1994 Availability: Windows, many Unix variants.
1995
1996
1997.. method:: file.write(str)
1998
1999 Write a string to the file. There is no return value. Due to buffering, the
2000 string may not actually show up in the file until the :meth:`flush` or
2001 :meth:`close` method is called.
2002
2003
2004.. method:: file.writelines(sequence)
2005
2006 Write a sequence of strings to the file. The sequence can be any iterable
2007 object producing strings, typically a list of strings. There is no return value.
2008 (The name is intended to match :meth:`readlines`; :meth:`writelines` does not
2009 add line separators.)
2010
2011Files support the iterator protocol. Each iteration returns the same result as
2012``file.readline()``, and iteration ends when the :meth:`readline` method returns
2013an empty string.
2014
2015File objects also offer a number of other interesting attributes. These are not
2016required for file-like objects, but should be implemented if they make sense for
2017the particular object.
2018
2019
2020.. attribute:: file.closed
2021
2022 bool indicating the current state of the file object. This is a read-only
2023 attribute; the :meth:`close` method changes the value. It may not be available
2024 on all file-like objects.
2025
2026
Georg Brandl4b491312007-08-31 09:22:56 +00002027.. XXX does this still apply?
Georg Brandl116aa622007-08-15 14:28:22 +00002028.. attribute:: file.encoding
2029
2030 The encoding that this file uses. When Unicode strings are written to a file,
2031 they will be converted to byte strings using this encoding. In addition, when
2032 the file is connected to a terminal, the attribute gives the encoding that the
2033 terminal is likely to use (that information might be incorrect if the user has
2034 misconfigured the terminal). The attribute is read-only and may not be present
2035 on all file-like objects. It may also be ``None``, in which case the file uses
2036 the system default encoding for converting Unicode strings.
2037
Georg Brandl116aa622007-08-15 14:28:22 +00002038
2039.. attribute:: file.mode
2040
2041 The I/O mode for the file. If the file was created using the :func:`open`
2042 built-in function, this will be the value of the *mode* parameter. This is a
2043 read-only attribute and may not be present on all file-like objects.
2044
2045
2046.. attribute:: file.name
2047
2048 If the file object was created using :func:`open`, the name of the file.
2049 Otherwise, some string that indicates the source of the file object, of the
2050 form ``<...>``. This is a read-only attribute and may not be present on all
2051 file-like objects.
2052
2053
2054.. attribute:: file.newlines
2055
2056 If Python was built with the :option:`--with-universal-newlines` option to
2057 :program:`configure` (the default) this read-only attribute exists, and for
2058 files opened in universal newline read mode it keeps track of the types of
2059 newlines encountered while reading the file. The values it can take are
2060 ``'\r'``, ``'\n'``, ``'\r\n'``, ``None`` (unknown, no newlines read yet) or a
2061 tuple containing all the newline types seen, to indicate that multiple newline
2062 conventions were encountered. For files not opened in universal newline read
2063 mode the value of this attribute will be ``None``.
2064
2065
Georg Brandl116aa622007-08-15 14:28:22 +00002066.. _typecontextmanager:
2067
2068Context Manager Types
2069=====================
2070
Georg Brandl116aa622007-08-15 14:28:22 +00002071.. index::
2072 single: context manager
2073 single: context management protocol
2074 single: protocol; context management
2075
2076Python's :keyword:`with` statement supports the concept of a runtime context
2077defined by a context manager. This is implemented using two separate methods
2078that allow user-defined classes to define a runtime context that is entered
2079before the statement body is executed and exited when the statement ends.
2080
2081The :dfn:`context management protocol` consists of a pair of methods that need
2082to be provided for a context manager object to define a runtime context:
2083
2084
2085.. method:: contextmanager.__enter__()
2086
2087 Enter the runtime context and return either this object or another object
2088 related to the runtime context. The value returned by this method is bound to
2089 the identifier in the :keyword:`as` clause of :keyword:`with` statements using
2090 this context manager.
2091
2092 An example of a context manager that returns itself is a file object. File
2093 objects return themselves from __enter__() to allow :func:`open` to be used as
2094 the context expression in a :keyword:`with` statement.
2095
2096 An example of a context manager that returns a related object is the one
2097 returned by ``decimal.Context.get_manager()``. These managers set the active
2098 decimal context to a copy of the original decimal context and then return the
2099 copy. This allows changes to be made to the current decimal context in the body
2100 of the :keyword:`with` statement without affecting code outside the
2101 :keyword:`with` statement.
2102
2103
2104.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
2105
Georg Brandl9afde1c2007-11-01 20:32:30 +00002106 Exit the runtime context and return a Boolean flag indicating if any exception
Georg Brandl116aa622007-08-15 14:28:22 +00002107 that occurred should be suppressed. If an exception occurred while executing the
2108 body of the :keyword:`with` statement, the arguments contain the exception type,
2109 value and traceback information. Otherwise, all three arguments are ``None``.
2110
2111 Returning a true value from this method will cause the :keyword:`with` statement
2112 to suppress the exception and continue execution with the statement immediately
2113 following the :keyword:`with` statement. Otherwise the exception continues
2114 propagating after this method has finished executing. Exceptions that occur
2115 during execution of this method will replace any exception that occurred in the
2116 body of the :keyword:`with` statement.
2117
2118 The exception passed in should never be reraised explicitly - instead, this
2119 method should return a false value to indicate that the method completed
2120 successfully and does not want to suppress the raised exception. This allows
2121 context management code (such as ``contextlib.nested``) to easily detect whether
2122 or not an :meth:`__exit__` method has actually failed.
2123
2124Python defines several context managers to support easy thread synchronisation,
2125prompt closure of files or other objects, and simpler manipulation of the active
2126decimal arithmetic context. The specific types are not treated specially beyond
2127their implementation of the context management protocol. See the
2128:mod:`contextlib` module for some examples.
2129
Georg Brandl9afde1c2007-11-01 20:32:30 +00002130Python's :term:`generator`\s and the ``contextlib.contextfactory`` decorator provide a
Georg Brandl116aa622007-08-15 14:28:22 +00002131convenient way to implement these protocols. If a generator function is
2132decorated with the ``contextlib.contextfactory`` decorator, it will return a
2133context manager implementing the necessary :meth:`__enter__` and
2134:meth:`__exit__` methods, rather than the iterator produced by an undecorated
2135generator function.
2136
2137Note that there is no specific slot for any of these methods in the type
2138structure for Python objects in the Python/C API. Extension types wanting to
2139define these methods must provide them as a normal Python accessible method.
2140Compared to the overhead of setting up the runtime context, the overhead of a
2141single class dictionary lookup is negligible.
2142
2143
2144.. _typesother:
2145
2146Other Built-in Types
2147====================
2148
2149The interpreter supports several other kinds of objects. Most of these support
2150only one or two operations.
2151
2152
2153.. _typesmodules:
2154
2155Modules
2156-------
2157
2158The only special operation on a module is attribute access: ``m.name``, where
2159*m* is a module and *name* accesses a name defined in *m*'s symbol table.
2160Module attributes can be assigned to. (Note that the :keyword:`import`
2161statement is not, strictly speaking, an operation on a module object; ``import
2162foo`` does not require a module object named *foo* to exist, rather it requires
2163an (external) *definition* for a module named *foo* somewhere.)
2164
2165A special member of every module is :attr:`__dict__`. This is the dictionary
2166containing the module's symbol table. Modifying this dictionary will actually
2167change the module's symbol table, but direct assignment to the :attr:`__dict__`
2168attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines
2169``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying
2170:attr:`__dict__` directly is not recommended.
2171
2172Modules built into the interpreter are written like this: ``<module 'sys'
2173(built-in)>``. If loaded from a file, they are written as ``<module 'os' from
2174'/usr/local/lib/pythonX.Y/os.pyc'>``.
2175
2176
2177.. _typesobjects:
2178
2179Classes and Class Instances
2180---------------------------
2181
2182See :ref:`objects` and :ref:`class` for these.
2183
2184
2185.. _typesfunctions:
2186
2187Functions
2188---------
2189
2190Function objects are created by function definitions. The only operation on a
2191function object is to call it: ``func(argument-list)``.
2192
2193There are really two flavors of function objects: built-in functions and
2194user-defined functions. Both support the same operation (to call the function),
2195but the implementation is different, hence the different object types.
2196
2197See :ref:`function` for more information.
2198
2199
2200.. _typesmethods:
2201
2202Methods
2203-------
2204
2205.. index:: object: method
2206
2207Methods are functions that are called using the attribute notation. There are
2208two flavors: built-in methods (such as :meth:`append` on lists) and class
2209instance methods. Built-in methods are described with the types that support
2210them.
2211
2212The implementation adds two special read-only attributes to class instance
2213methods: ``m.im_self`` is the object on which the method operates, and
2214``m.im_func`` is the function implementing the method. Calling ``m(arg-1,
2215arg-2, ..., arg-n)`` is completely equivalent to calling ``m.im_func(m.im_self,
2216arg-1, arg-2, ..., arg-n)``.
2217
2218Class instance methods are either *bound* or *unbound*, referring to whether the
2219method was accessed through an instance or a class, respectively. When a method
2220is unbound, its ``im_self`` attribute will be ``None`` and if called, an
2221explicit ``self`` object must be passed as the first argument. In this case,
2222``self`` must be an instance of the unbound method's class (or a subclass of
2223that class), otherwise a :exc:`TypeError` is raised.
2224
2225Like function objects, methods objects support getting arbitrary attributes.
2226However, since method attributes are actually stored on the underlying function
2227object (``meth.im_func``), setting method attributes on either bound or unbound
2228methods is disallowed. Attempting to set a method attribute results in a
2229:exc:`TypeError` being raised. In order to set a method attribute, you need to
2230explicitly set it on the underlying function object::
2231
2232 class C:
2233 def method(self):
2234 pass
2235
2236 c = C()
2237 c.method.im_func.whoami = 'my name is c'
2238
2239See :ref:`types` for more information.
2240
2241
2242.. _bltin-code-objects:
2243
2244Code Objects
2245------------
2246
2247.. index:: object: code
2248
2249.. index::
2250 builtin: compile
2251 single: __code__ (function object attribute)
2252
2253Code objects are used by the implementation to represent "pseudo-compiled"
2254executable Python code such as a function body. They differ from function
2255objects because they don't contain a reference to their global execution
2256environment. Code objects are returned by the built-in :func:`compile` function
2257and can be extracted from function objects through their :attr:`__code__`
2258attribute. See also the :mod:`code` module.
2259
2260.. index::
2261 builtin: exec
2262 builtin: eval
2263
2264A code object can be executed or evaluated by passing it (instead of a source
2265string) to the :func:`exec` or :func:`eval` built-in functions.
2266
2267See :ref:`types` for more information.
2268
2269
2270.. _bltin-type-objects:
2271
2272Type Objects
2273------------
2274
2275.. index::
2276 builtin: type
2277 module: types
2278
2279Type objects represent the various object types. An object's type is accessed
2280by the built-in function :func:`type`. There are no special operations on
2281types. The standard module :mod:`types` defines names for all standard built-in
2282types.
2283
2284Types are written like this: ``<type 'int'>``.
2285
2286
2287.. _bltin-null-object:
2288
2289The Null Object
2290---------------
2291
2292This object is returned by functions that don't explicitly return a value. It
2293supports no special operations. There is exactly one null object, named
2294``None`` (a built-in name).
2295
2296It is written as ``None``.
2297
2298
2299.. _bltin-ellipsis-object:
2300
2301The Ellipsis Object
2302-------------------
2303
Georg Brandlcb8ecb12007-09-04 06:35:14 +00002304This object is commonly used by slicing (see :ref:`slicings`). It supports no
2305special operations. There is exactly one ellipsis object, named
Georg Brandl116aa622007-08-15 14:28:22 +00002306:const:`Ellipsis` (a built-in name).
2307
2308It is written as ``Ellipsis`` or ``...``.
2309
2310
2311Boolean Values
2312--------------
2313
2314Boolean values are the two constant objects ``False`` and ``True``. They are
2315used to represent truth values (although other values can also be considered
2316false or true). In numeric contexts (for example when used as the argument to
2317an arithmetic operator), they behave like the integers 0 and 1, respectively.
2318The built-in function :func:`bool` can be used to cast any value to a Boolean,
2319if the value can be interpreted as a truth value (see section Truth Value
2320Testing above).
2321
2322.. index::
2323 single: False
2324 single: True
2325 pair: Boolean; values
2326
2327They are written as ``False`` and ``True``, respectively.
2328
2329
2330.. _typesinternal:
2331
2332Internal Objects
2333----------------
2334
2335See :ref:`types` for this information. It describes stack frame objects,
2336traceback objects, and slice objects.
2337
2338
2339.. _specialattrs:
2340
2341Special Attributes
2342==================
2343
2344The implementation adds a few special read-only attributes to several object
2345types, where they are relevant. Some of these are not reported by the
2346:func:`dir` built-in function.
2347
2348
2349.. attribute:: object.__dict__
2350
2351 A dictionary or other mapping object used to store an object's (writable)
2352 attributes.
2353
2354
2355.. attribute:: instance.__class__
2356
2357 The class to which a class instance belongs.
2358
2359
2360.. attribute:: class.__bases__
2361
2362 The tuple of base classes of a class object. If there are no base classes, this
2363 will be an empty tuple.
2364
2365
2366.. attribute:: class.__name__
2367
2368 The name of the class or type.
2369
2370.. rubric:: Footnotes
2371
2372.. [#] Additional information on these special methods may be found in the Python
2373 Reference Manual (:ref:`customization`).
2374
2375.. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
2376 similarly for tuples.
2377
2378.. [#] They must have since the parser can't tell the type of the operands.
2379
2380.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
2381 element is the tuple to be formatted.
2382
2383.. [#] These numbers are fairly arbitrary. They are intended to avoid printing endless
2384 strings of meaningless digits without hampering correct use and without having
2385 to know the exact precision of floating point values on a particular machine.
2386
2387.. [#] :func:`file` is new in Python 2.2. The older built-in :func:`open` is an alias
2388 for :func:`file`.
2389
2390.. [#] The advantage of leaving the newline on is that returning an empty string is
2391 then an unambiguous EOF indication. It is also possible (in cases where it
2392 might matter, for example, if you want to make an exact copy of a file while
2393 scanning its lines) to tell whether the last line of a file ended in a newline
2394 or not (yes this happens!).