Why tuple with one item is no tuple

deelan ggg at zzz.it
Tue Mar 15 17:21:25 CET 2005


Gregor Horvath wrote:
> Hi,
> 
>  >>>type(['1'])
> <type 'list'>
> 
>  >>>type(('1'))
> <type 'str'>
> 
> I wonder why ('1') is no tuple????
> 
> Because I have to treat this "special" case differently in my code.

you need to tell python that ('1') isn't a string inside
a couple parens but a tuple, look:

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

if there's no ambiguity you can omit the parens:

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

HTH,
deelan

-- 
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://blog.deelan.com/> .



More information about the Python-list mailing list