Python Gotcha's?

Steve Howell showell30 at yahoo.com
Fri Apr 6 00:54:06 EDT 2012


On Apr 5, 9:28 pm, rusi <rustompm... at gmail.com> wrote:
> On Apr 5, 4:06 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>
> > Steven D'Aprano <steve+comp.lang.pyt... at pearwood.info> wrote:
> > > JSON expects double-quote marks, not single:
> > >     v = json.loads("{'test':'test'}")  fails
> > >     v = json.loads('{"test":"test"}')  succeeds
>
> > You mean JSON expects a string with valid JSON?
> > Quelle surprise.
>
> Are there languages (other than python) in which single and double
> quotes are equivalent?
>
> [No I dont claim to know all the languages out there, just that I dont
> know any other language which allows single and double quotes to be
> interconvertible like python does]

Python doesn't treat single quotes and double quotes in *exactly* the
same manner, because the choice of outer quotes affects whether you
need to escape the outer quote characters inside the string.  But I
don't want to be overly literal--I think I know what you mean by
"equivalent" here.

JS, YAML, and HTML are pretty similar to Python with respect to single
vs. double, as far as I know/remember/care.

Perl, Ruby, and CoffeeScript have the tradition that single quotes are
interpreted literally, whereas double quotes allow for interpolation
of things within the string.  This is roughly inspired by English,
where one says things like "Double quotes are an 'enclosing syntax'
for single quotes."  [Yes, I'm just making that up.  Sounds plausible,
right?]

Both Ruby and CoffeeScript support triple quote syntax similar to
Python.

C uses double quotes for strings, as opposed to single quotes for
characters.

Java only allows double quotes for strings.

I'll wager a guess that if you took any two programming languages
(including declarative languages like SQL/HTML) and compared how they
represented string literals, there would be at least one thing
different between them, and that difference would be a fairly
arbitrary design decision.  There are probably exceptions, but
languages that have the exact same quoting rules would probably be
close dialects of each other in other respects beyond quoting.

I'll also wager a guess that at least one thing I said above was
wrong, and that's a testament to the arcane nature of representing
string literals (as well as my own lack of mental capacity for
juggling all these different rules in my brain).  And that's just in
ASCII with an American English bias.  Throw in Unicode--that's when
things get really confusing!

I'm happy to stand corrected on any fact above.  Withhold insults,
though.  I already know that string literal syntax makes me feel
stupid--no need to rub it in.







More information about the Python-list mailing list