one-element tuples

Ned Batchelder ned at nedbatchelder.com
Sun Apr 10 21:00:00 EDT 2016


On Sunday, April 10, 2016 at 8:48:49 PM UTC-4, Fillmore wrote:
> On 04/10/2016 08:31 PM, Ben Finney wrote:
> > Can you describe explicitly what that "discontinuation point" is? I'm
> > not seeing it.
> 
> Here you go:
> 
>  >>> a = '"string1"'
>  >>> b = '"string1","string2"'
>  >>> c = '"string1","string2","string3"'
>  >>> ea = eval(a)
>  >>> eb = eval(b)
>  >>> ec = eval(c)
>  >>> type(ea)
> <class 'str'>   <--- HERE !!!!
>  >>> type(eb)
> <class 'tuple'>
>  >>> type(ec)
> <class 'tuple'>
> 
> I can tell you that it exists because it bit me in the butt today...
> 
> and mind you, I am not saying that this is wrong. I'm just saying that it surprised me.

Perhaps the extra complication of eval is confusing things.  This:

    >>> a = '"string1"'
    >>> ea = eval(a)
    >>> type(ea)
    <class 'str'>

is the same as:

    >>> type("string1")
    <class 'str'>

Does that surprise you?  "string1" sure looks like a plain-old string to
me, I'm not sure why you would think it would be a tuple.

Your three expressions are:

    ea = "string1"
    eb = "string1","string2"
    ec = "string1","string2","string3"

ea is a string, eb is a two-element tuple (both elements are strings), 
and ec is a three-element tuple (all elements are strings).

As others have said, commas make tuples.  Your first expression has no
commas (and no parens!) so it is not a tuple.

--Ned.



More information about the Python-list mailing list