5 ms·
> Black prefers [i.e. converts] double quotes (" and """) over single quotes (' and '''). Can someone explain this to me? Why would you ever prefer " over ' in
by formerly_proven 5y ago
> Black prefers [i.e. converts] double quotes (" and """) over single quotes (' and ''').
Can someone explain this to me? Why would you ever prefer " over ' in a language where both can be used equally?
- nopenopenopeno 5y agoIt’s easier and faster to differentiate between “ and ` than between ‘ and `.
- floober 5y agoUniformity, presumably
- odiroot 5y agoI prefer them too. Makes it easy to notice than single ones. It's just a personal preference, there's no big logic behind it.
- Teongot 5y agoBecause "don't" is easier to read than 'don\'t'
- JackFr 5y agoWhen you’re embedding SQL in a string, a you use ‘ a lot more than “. Not the only use case, but one to give some consideration.
- throwaway894345 5y agoI think that’s only MySQL. Postgres uses double quotes for identifiers and single quotes for literals. 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.
- iqanq 5y agoAnd 'do not' is easier to read than both :)
- shpx 5y agoBlack will use single quotes if your string contains double quotes and no single quotes. It'll reformat 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.
- JimDabell 5y agoIf you use proper apostrophes instead of ASCII apostrophe / single quote, you don’t have to escape anything and you get better typography. "don’t" is easier to read than both. If you’re on a Mac, Option-Shift-] will type it.
- breput 5y agoA couple of possible reasons: * Double quotes are more visible, especially for triple quotes. * It makes it easier to have text without having to manually escape apostrophes.
- teddyh 5y agoBecause 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.
- tsimionescu 5y agoExcept 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.
- teddyh 5y ago(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\';'
- tsimionescu 5y agoIf 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';"
- teddyh 5y ago> 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.
- drekipus 5y agoIt's a tic for me too, as I always prefer '. 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.
- deleted 5y ago[deleted]
- ambivalence 5y agoThis was decided very early in the project, the discussion can be read here: https://github.com/psf/black/issues/51 https://github.com/psf/black/issues/51
- wging 5y agoAlso documented more permanently here: https://black.readthedocs.io/en/latest/the_black_code_style/current_style.html#strings https://black.readthedocs.io/en/latest/the_black_code_style/...
- formerly_proven 5y agoDid you lock that issue before posting it here? Is this some kind of sore/forbidden topic for the project? Edit: I suppose the question is superfluous seeing how many replies there are just here.
- ambivalence 5y agoYes, and yes. Edit: and yes.
- indymike 5y agoThis is simple: the default is picked, so now no one has to argue about it.
- TameAntelope 5y agoArguing over Black formatting choices is a little ironic, isn’t it? The whole point is that they’re all arbitrary so just let the defaults win, and adjust yourself instead.
- WillDaSilva 5y agoThe presence and depth of the debate about this formatting choice suggests that it isn't arbitrary. Many have provided solid reasons for why one option should be preferred over the other. They aren't merely articulating their opinions; they are also providing objective reasons for why their opinion is valid.
- TameAntelope 5y agoEverything about formatting is arbitrary, as long as it's consistent in a given codebase to the point of predictability (e.g. 90% consistent, not necessarily 100% consistent). 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.
- faangiq 5y agoIn some languages single vs double has different functionality.
- kstrauser 5y agoThat was the thing that bugged me at first, but other people on my team loved it. Some things bugged other people on my team, even though I love them. If a tool can manage to annoy everyone about the same amount, but in different areas, it’s doing something right. 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.
- mixmastamyk 5y agoTry nero from PyPI instead, I’ll upgrade to stable shortly.
- rirze 5y agoLast commit was from 2019? It sounds like you're not automating the patch for every black release...
- mixmastamyk 5y agoThings were factored a few times, wasn’t a consistent patch. Also I didn’t care for a few of the changes so didn’t upgrade yet. However stable should be a good target for automation.
- HALtheWise 5y agoAdditionally, standardizing all quotes makes searching for a specific string literal across a codebase easier, since you don't need regex to match against both quote types. Once you've decided to standardize strings, it doesn't matter much which one you pick. Personally, I still type single quotes in almost all cases, and just let Black reformat to double quotes to save shift key presses.
- hadcomplained 5y agoThe other way around (converting double quoted strings to single quoted ones) would be more understandable, as that is what str.__repr__ prefers: >>> "string" 'string' I sense a bloated ego of the author in Black's decisions -- to contradict with the language's preferred formatting style.