Why the expression "(1)" is not an one-arity tuple, but int ?

Wolodja Wentland wentland at cl.uni-heidelberg.de
Fri Dec 4 07:29:46 EST 2009


On Fri, Dec 04, 2009 at 15:17 +0300, Петров Александр wrote:
> In my code I try to use a generic approach to work with tuples. Let
> "X" be a tuple.
> When I want to access a first element of a tuple, I can write: "X[0]".
> And that is really working when X is a n-arity tuple, with n>1 (for
> example "foo( (1,2,3) )" ).
> But when I call my library function with a 1-arity tuple (for example
> "foo( (1) )" ) I have an error:
> 
> TypeError: 'int' object is unsubscriptable
> 
> How could I tell Python that "(1)" is not an integer, but an one-arity tuple ?

The following might clarify the issue:

>>> t = (1)
>>> type(t)
<type 'int'>
>>> t = (1,)
>>> type(t)
<type 'tuple'>
>>> t = 1,
>>> type(t)
<type 'tuple'>

It is the ',' not the '(' and ')' ...


-- 
  .''`.     Wolodja Wentland    <wentland at cl.uni-heidelberg.de> 
 : :'  :    
 `. `'`     4096R/CAF14EFC 
   `-       081C B7CD FF04 2BA9 94EA  36B2 8B7F 7D30 CAF1 4EFC
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20091204/7dd5830e/attachment-0001.sig>


More information about the Python-list mailing list