one-element tuples
BartC
bc at freeuk.com
Mon Apr 11 07:15:49 EDT 2016
On 11/04/2016 01:48, 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.
I think this shows more clearly what you mean:
a = 10 # int
b = 10, # tuple
c = 10,20 # tuple
d = 10,20,30 # tuple
The difference between a and b is that trailing comma. A bit of a
kludge, but it's enough to distinguish between a single value (x), and a
one-element tuple (x,).
In this case, you might call it a discontinuity in the syntax as, when
you go from d to c, you remove ",30", including the comma, but when
going from c to b, you remove only "20".
But this can be fixed if tuples are written like this:
a = 10 # int
b = 10, # tuple
c = 10,20, # tuple
d = 10,20,30, # tuple
Now, you remove "30," then "20,". No exception.
Of course this doesn't help you parsing typical input which uses commas
as separators, not terminators!
--
Bartc
More information about the Python-list
mailing list