6 ms·
> Larry Hastings pointed out that PHP strings are format strings by default and "the script kids love it." This seems like an odd argument. PHP makes a clear d
by jorams 6y ago
> Larry Hastings pointed out that PHP strings are format strings by default and "the script kids love it."
This seems like an odd argument. PHP makes a clear distinction between string literals with single quotes and those with double quotes, where only double quotes do variable substitution and escape sequences. It's very common for PHP projects to default to single quotes and only use double quotes when their special handling is actually used.
- zem 6y agoruby does this too, and I think it's a great design. I know it's too late for python to go that route, but I wish they had way back when, rather than having single and double quotes be the same thing.
- BerislavLopac 6y agoPython's mechanism for this is using a single-character prefix, which would still be needed even if it followed the Ruby/PHP syntax -- simply because Python has at least four different types of string literals: * f"f-strings", which do both substitution and escaping * regular "strings", which do escaping but no substitution * r"r-strings" (for "raw"), which do neither * b"b-strings", which are actually not strings but sequences of bytes
- pansa2 6y agoThere are also `fr`-prefixed strings, which do substitution but no escaping, and `br`-prefixed raw byte sequences.
- BerislavLopac 6y agoExcellent point, thank you! There are also the u"u-strings" which were reintroduced to reduce the workload when converting the Python 2 programs; unfortunately they forgot to do the same for the relatively common (at least when defining regular expressions) ru"ru-strings" and ur"ur-strings"...
- pansa2 6y agoDoes Ruby support strings that do escaping but not variable interpolation? It's interesting if not, because that's the behavior of Python's "normal" strings.
- wruza 6y agoSo does perl, sh, etc, and god knows how long it goes back in time, and even when a language doesn’t, it always strikes as a frivolous move to make two characters to mean the same. Either do 's or "s but both... it’s just a fuel for style(9) wars. The only cases when it’s reasonable is sqlite-like ones, when your goal is to support as many legacy habits as possible.
- larzang 6y agoAdditionally sprintf (basically .format) is strongly preferred to double quotes in many projects for readability and highlighting explicit intent.