Kaydet (Commit) f6379206 authored tarafından Ezio Melotti's avatar Ezio Melotti

#14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.

üst a0933128
...@@ -423,17 +423,31 @@ A tuple consists of a number of values separated by commas, for instance:: ...@@ -423,17 +423,31 @@ A tuple consists of a number of values separated by commas, for instance::
... u = t, (1, 2, 3, 4, 5) ... u = t, (1, 2, 3, 4, 5)
>>> u >>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable:
... t[0] = 88888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects:
... v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])
As you see, on output tuples are always enclosed in parentheses, so that nested As you see, on output tuples are always enclosed in parentheses, so that nested
tuples are interpreted correctly; they may be input with or without surrounding tuples are interpreted correctly; they may be input with or without surrounding
parentheses, although often parentheses are necessary anyway (if the tuple is parentheses, although often parentheses are necessary anyway (if the tuple is
part of a larger expression). part of a larger expression). It is not possible to assign to the individual
items of a tuple, however it is possible to create tuples which contain mutable
Tuples have many uses. For example: (x, y) coordinate pairs, employee records objects, such as lists.
from a database, etc. Tuples, like strings, are immutable: it is not possible
to assign to the individual items of a tuple (you can simulate much of the same Though tuples may seem similar to lists, they are often used in different
effect with slicing and concatenation, though). It is also possible to create situations and for different purposes.
tuples which contain mutable objects, such as lists. Tuples are :term:`immutable`, and usually contain an heterogeneous sequence of
elements that are accessed via unpacking (see later in this section) or indexing
(or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
Lists are :term:`mutable`, and their elements are usually homogeneous and are
accessed by iterating over the list.
A special problem is the construction of tuples containing 0 or 1 items: the A special problem is the construction of tuples containing 0 or 1 items: the
syntax has some extra quirks to accommodate these. Empty tuples are constructed syntax has some extra quirks to accommodate these. Empty tuples are constructed
...@@ -462,8 +476,6 @@ variables on the left to have the same number of elements as the length of the ...@@ -462,8 +476,6 @@ variables on the left to have the same number of elements as the length of the
sequence. Note that multiple assignment is really just a combination of tuple sequence. Note that multiple assignment is really just a combination of tuple
packing and sequence unpacking. packing and sequence unpacking.
.. XXX Add a bit on the difference between tuples and lists.
.. _tut-sets: .. _tut-sets:
......
...@@ -900,6 +900,7 @@ Richard Walker ...@@ -900,6 +900,7 @@ Richard Walker
Larry Wall Larry Wall
Kevin Walzer Kevin Walzer
Greg Ward Greg Ward
Zachary Ware
Barry Warsaw Barry Warsaw
Steve Waterbury Steve Waterbury
Bob Watson Bob Watson
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment