What I like about the Black philosophy is that it wants to make code style _uninteresting_. People should think about other things, not formatting. That's a great goal. So it seems to me that the best style choice is the most _boring_ choice. The least creative, least novel way. It should try to avoid inventing new formatting algorithms.
What's the most boring way to format a string literal?
The way the language already does it. The way every Python programmer has already seen string literals formatted from the very first day they started typing things into a Python interpreter.
Even if half of us liked single-quotes and half of us liked double-quotes, you can guarantee that every Python programmer on the planet has seen and lived with strings that are formatted the repr() way, including the double-quote fans. You can't guarantee the opposite.
No one could fault you for doing it the repr() way. Blame Guido :) he made that choice decades ago, and everyone has already had to make their peace with it. It's a solved problem. For anyone writing a program that emits Python code, it's the default way to format a string. It's the least assailable option, and that's a good thing.
How does that sit with you?
http://www.grymoire.com/Unix/Quote.html#uh-1
"When you need to quote several character at once, you could use several backslashes. This is ugly but works. It is easier to use pairs of quotation marks to indicate the start and end of the characters to be quoted. Inside the single quotes, you can include almost all meta-characters"
So some of us associate single quotes with proper const strings, with no variable expansion, command substitution or other interpreter hanky-panky.
I get your specific comment may be a bit sarcastic, given the ancestry - but I make this one more for younger developers I see that don’t always filter that ;)
Quotes: We have a difference of opinion over what constitutes an established convention. If the language's own way of displaying strings has been stable for its entire history, I consider that established. A reasonable choice would have been to do what repr() does (single-quotes unless the string contains a literal single-quote) or a simplified version of it (single-quotes always), not the opposite of what the language itself does.
(I wouldn't care so much about this except that I've really wanted a tool like Black for a while and greatly appreciate the philosophy!)
I can't speak for the readme, but:
$ cat test.py
x = [1,2,3]
print(x[1: 3])
$ black test.py
reformatted test.py
$ cat test.py
x = [1, 2, 3]
print(x[1:3])I tried out black and it does the following (the file originally had no spaces):
x = a + b
x = m[a:b]
x = m[a + 1 : b]
x = m[a:-b]
x = m[a : 1 - b]
I would personally still write m[a + 1:b], I think, but black's approach is totally defensible. (I guess I would really write m[a+1:b], and black would rightly correct me.)I usually avoid this problem by adding extra parentheses:
m[(a + 1):b]
f(x=(y + 1))Spacings that don't conflict with precedence are m[a+1:b], m[a+1 : b], or m[a + 1 : b]. While the middle one makes precedence obvious, all three are acceptable. black picks the latter, and I think it picks that one because of another rule that it should prefer writing a + 1 instead of a+1.
Double quotes are also the convention in english to delineate a literal, so I would argue it's more obvious.
I'll grant you keyboard presses though. There's an obvious advantage to single quotes here, but consider why this is the case. To make the use of "apostrophe" more efficient since it appears far more often than a double quote in english. I suppose it's pragmatic to leverage this advantage in code where string quoting is extremely common...
Perhaps double quoting is a bias I've developed writing code, but I suspect it's actually a bias I carried over from reading and writing english and what simply seemed more obvious.
Language design than supports both makes me crazy. Pick one and enforce it!
The visual noise complaint is interesting. Do you also consider the letter W to be more noisy than the letter V? Should we discourage the use of noisy letters in the alphabet?
The difference is that " and ' are equally usable options in the context we're talking about. Quotes are very common, so the visual noise adds up when your screen is full of quote marks. Given that they mean the same thing, and one is both harder to type and harder to read, it makes sense to prefer the other.
Nailed it.
I thought the same. I guess you could just modify your programming font to make the double quotes really tiny :)
raise KeyError('"%s" not found.' % name)
f'The flange is elevated by {flange["spronge"]} degrees of spronge.'
I don't normally use many contractions or possessives in my code, but am willing to admit it happens occasionally.
I'd like «guillemets» but that's going to have to be self-serve...
set_color('black','white')
And double quotes for strings meant to be read by a human: print("Hello there. How are you?")
And an example with both forms: syslog('warning',"unit %s spin rate %f too low",u,rate)
For me, it's an indication of how I expect the string to be used.PHP does treat single and double quotes differently. Contents of single quotes strings are not parsed for variable substitution.
This leads to the following difference between the PEP 8 recommendation and Black:
PEP 8 accepts both of these:
ham[lower+offset : upper+offset]
ham[lower + offset : upper + offset]
While Black just uses: ham[lower + offset : upper + offset]
Also, this is one that PEP 8 doesn't have a example on, but I think this would be one of the cases in which the space wasn't necessary: slice[a.b : c.d]
The "." operator binds so tightly in my mind that it doesn't need the spaces around the ":" to disambiguate. It's at the same precedence level as subscription and function call, and higher precedence than unary + and -.After typing this all out, I think that it might be the case that the bigger difference is actually in how binary operators are treated. It looks like Black always puts whitespace around binary operators, which contradicts PEP 8's recommendation that you are allowed to vary spacing around binary operators to make precedence clear.
Since Black does allow for leaving in extraneous parentheses to make precedence clear, I wonder why it doesn't allow varying the space around binary operators as well? Of course, it should enforce that the spacing actually does match the precedence, and that the spacing is consistent within an expression. That would allow the following examples which PEP 8 lists as "Yes":
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
While right now Black rewrites them as follows, which PEP 8 lists as "No" (though my reading is that varying the spacing like this is optional, so the following could be accepted as well): x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)With this in mind, it has to enforce a rule around operators. Since any operand might be complex, it's more robust to default to spaces around operands always. Otherwise we would inevitably end up hugging operands with an operator that humans consider too tight. And since that's subjective, there is actually no rule that we can hard-code about that.
Why not? I mean, I never built a formatter so idk about the complexity involved, but to me it seems that if Black had a rule like "if you see `a+b` (no spaces), just leave it alone", applying the rule twice shouldn't change anything.
(though I imagine that the interactions of such rules could become hard to understand...)
The language itself doesn't have an established standard in terms of string quote usage. If it did, Black would follow it. What repr() does is a weak indicator and how the documentation is written is random, there was not only no enforcement as to which quotes to use, there wasn't even a recommendation. Black standardizes on double quotes since it has clear benefits whereas the other option does not.