[python 2.4] unable to construct tuple with one item

Facundo Batista facundo at taniquetil.com.ar
Sun May 6 10:55:43 EDT 2007


Vyacheslav Maslov wrote:

> So, the main question is why using syntax like [X] python constuct list with
> one item, but when i try to construct tuple with one item using similar
> syntax (X) python do nothing?

Because what determines that you actually have a tuple is the comma, not
the parenthesis. See the following explanations.


Both empty tuples, one without arguments, the second also without
arguments:

>>> tuple()
()
>>> tuple(())
()


Constructing a tuple with one argument, which is an iterable (actually,
another tuple):

>>> tuple((4,))
(4,)


The following is not allowed, because you're calling tuple() with one
argument, an int 4, and ints are not iterable.

>>> tuple(4,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable


Note how we also can construct tuples by using the commas, and not the
parenthesis:

>>> 3,4
(3, 4)
>>> a = 5,6
>>> type(a)
<type 'tuple'>
>>> c,d = a
>>> c
5
>>> d
6


Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/





More information about the Python-list mailing list