one-element tuples
Stephen Hansen
me+python at ixokai.io
Sun Apr 10 21:07:46 EDT 2016
On Sun, Apr 10, 2016, at 05:48 PM, 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"'
Here, "a" is a string that contains a double quoted string. So if you
evaluate it, you get a string.
> >>> b = '"string1","string2"'
Here, "b" is a string that contains two double quoted strings separated
by a comma. So if you evaluate it, you get a tuple of two strings.
> >>> c = '"string1","string2","string3"'
This is as above, but with three items.
With that in mind:
> >>> ea = eval(a)
> >>> eb = eval(b)
> >>> ec = eval(c)
> >>> type(ea)
> <class 'str'>
> >>> type(eb)
> <class 'tuple'>
> >>> type(ec)
> <class 'tuple'>
This should all be expected. The commas, when you evaluate them, are in
B&C making tuples. There's only a single string in A, so you get a
string. If you wanted a one item tuple, you would have written:
>>> a = '"string1",'
Note the trailing comma.
> 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.
If the above doesn't explain it, then I still don't understand what
you're finding surprising and what you'd expect otherwise.
---Stephen
m e @ i x o k a i . i o
More information about the Python-list
mailing list