Most probably a stupid question, but I still want to ask

Stephen Hansen me+python at ixokai.io
Sun Apr 10 19:30:24 EDT 2016


On Sun, Apr 10, 2016, at 03:51 PM, Fillmore wrote:
> 
> let's look at this:
> 
> $ python3.4
> Python 3.4.0 (default, Apr 11 2014, 13:05:11)
> [GCC 4.8.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> line1 = '"String1" | bla'
>  >>> parts1 = line1.split(" | ")
>  >>> parts1
> ['"String1"', 'bla']
>  >>> tokens1 = eval(parts1[0])
>  >>> tokens1
> 'String1'
>  >>> tokens1[0]
> 'S'
> 
> and now this
> 
>  >>> line2 = '"String1","String2" | bla'
>  >>> parts2 = line2.split(" | ")
>  >>> tokens2 = eval(parts2[0])

I *THINK* what you're asking is why this returns a tuple, where in the
first eval you got a string. The answer is because commas create tuples
(not parens), so:

    "String1", "String2"

is a tuple expression. Whereas:

    "String1"

is a string expression. 

> the question is: at which point did the language designers decide to
> betray the
> "path of least surprise" principle and create a 'discontinuity' in the
> language?

There's nothing inconsistent or surprising going on besides you doing
something vaguely weird and not really expressing what you find
surprising.

--Stephen
m e @ i x o k a i . i o



More information about the Python-list mailing list