News for hackers. Stuff that matters.
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Saturday January 29, 2022 @04:16PM (#30130562)
Because most commonly used languages use double quotes, not single quotes, for string literals. Some languages can use both, either interchangeably or with slightly different semantics, but others either uses single quotes for other purposes, or do not use them for anything at all. Therefore, double quotes for strings look more normal, so to speak, in source code.
Re:Black, the uncompromising Python code formatter, is stable by tsimionescu on Saturday January 29, 2022 @05:27PM (#30131211)
Except that in the language you're most likely to mix with Python, SQL, strings are in single quotes (double quotes are for identifiers like column names, especially if they're case sensitive). So it's probably a bad choice on Black's part.
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Saturday January 29, 2022 @07:12PM (#30132080)
(MySQL, notoriously, uses double quotes for strings.)

However, does not Python and standard SQL using different quote characters make it easier? I.e.

  query = "SELECT column_a FROM a_table WHERE column_b = 'foo';"
compared to this, if single quotes were enforced:

  query = 'SELECT column_a FROM a_table WHERE column_b = \'foo\';'
Re:Black, the uncompromising Python code formatter, is stable by tsimionescu on Saturday January 29, 2022 @08:24PM (#30132703)
If it's useful to use the same string literal character "as everyone else", it would be useful to use the same as a language you are very likely to see combined - so, single quote.

On the other hand, if it's useful to have minimum need of escaping, then single quote is still better, because in SQL it's much more common to quote identifier names than to have literal strings.

  query = 'SELECT "columnA" FROM "aTable" WHERE "columnB" = \'foo\';'
compared to

  query = "SELECT \"columnA\" FROM \"aTable\" WHERE \"columnB\" = 'foo';"
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Monday January 31, 2022 @02:49AM (#30145553)
> it would be useful to use the same as a language you are very likely to see combined - so, single quote.

Why? It’s obvious that it causes issues as I showed, and yet you simplay claim the opposite without argument? The difference between other programming languages and SQL is, of course, that it’s very common to have embedded SQL code in your program, but not common to have embedded strings of code in another programming language.

> in SQL it's much more common to quote identifier names than to have literal strings.

That is certainly not true. The kind of SQL you quote is only found in auto-generated SQL by ORMs and the like, and those are not likely to exist as strings in code. To use your example, this is the normal SQL way to write that:

  query = "SELECT columnA FROM aTable WHERE columnB = 'foo';"
I.e. no double quotes needed; no need to escape anything.
Re:Black, the uncompromising Python code formatter, is stable by xigoi on Saturday January 29, 2022 @07:22PM (#30132147)
Using double quotes makes it easier to have single quotes inside the string literals.