Can someone explain this to me? Why would you ever prefer " over ' in a language where both can be used equally?
Can someone explain this to me? Why would you ever prefer " over ' in a language where both can be used equally?
But to be fair I think it's because 1. Double quotes are ""universally"" strings in other languages 2. Having apostrophes gets annoying.
The great benefit really is that I can write single word strings just fine, and black will adjust it for me. I get my cake, and so do the others.
print("\"Hello\" he said")
to print('"Hello" he said')
I think pretty much all formatters for languages that let you use either quote do this. It makes sense.EDIT: I misinterpreted that single quote as a backtick. In any case, both single and double quotes are common in SQL, but single quotes are a bit more common.
If you’re on a Mac, Option-Shift-] will type it.
The whole point is that they’re all arbitrary so just let the defaults win, and adjust yourself instead.
To elaborate a bit, sure, there are arguments for everything talked about here, and they're valid, but the marginal utility of any one choice is dwarfed by opting out of the arguments entirely, and just going with whatever Black has selected as a default.
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\';'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';"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.Edit: I suppose the question is superfluous seeing how many replies there are just here.
And while it’s still kind of strange that `a = “foo”` looks different than `repr(a)`, after a while of using Black I don’t notice it anymore.
* Double quotes are more visible, especially for triple quotes.
* It makes it easier to have text without having to manually escape apostrophes.
Personally, I still type single quotes in almost all cases, and just let Black reformat to double quotes to save shift key presses.
>>> "string"
'string'
I sense a bloated ego of the author in Black's decisions -- to contradict with the language's preferred formatting style.