Python Gotcha's?

Steve Howell showell30 at yahoo.com
Thu Apr 5 21:20:49 EDT 2012


On Apr 5, 6:03 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Thu, 05 Apr 2012 23:08:11 +0200, André Malo wrote:
> > * Steven D'Aprano wrote:
>
> >> For a 21st century programming language or data format to accept only
> >> one type of quotation mark as string delimiter is rather like having a
> >> 21st century automobile with a hand crank to start the engine instead
> >> of an ignition. Even if there's a good reason for it (which I doubt),
> >> it's still surprising.
>
> > Here's a reason: KISS.
>
> KISS is a reason *for* allowing multiple string delimiters, not against
> it. The simplicity which matters here are:
>
> * the user doesn't need to memorise which delimiter is allowed, and
>   which is forbidden, which will be different from probably 50% of
>   the other languages he knows;

Exactly.  One of the reasons that human use computers in the first
place is that we have flawed memory with respect to details,
especially arbitrary ones.  It's the job of the computer to make our
lives easier.

> * the user can avoid the plague of escaping quotes inside strings
>   whenever he needs to embed the delimiter inside a string literal.
>

Unlike JSON, JS itself allows '"' and "'", although its canonical
representation of the latter is '\''.

Having said that, I don't mind that JSON is strict; I just hate that
certain JSON parsers give cryptic messages on such an obvious gotcha.

> This is the 21st century, not 1960, and if the language designer is
> worried about the trivially small extra effort of parsing ' as well as "
> then he's almost certainly putting his efforts in the wrong place.
>

FWIW the JSON parser in Javascript is at least capable of giving a
precise explanation in its error message, which put it ahead of
Python:

 >config = "{'foo': 'bar'}"
 "{'foo': 'bar'}"
 >JSON.parse(config)
 SyntaxError: Unexpected token '

(Tested under Chrome and node.js, both based on V8.)

Here's Python:

 >>> config = "{'foo': 'bar'}"
 >>> import json
 >>> json.loads(config)
 [...]
 ValueError: Expecting property name: line 1 column 1 (char 1)

(Python's implementation at least beats JS by including line/column
info.)



More information about the Python-list mailing list