3 ms·
You could easily use a + operator then. I find the behavior surprising. I would expect a syntax error. You get a syntax error if you write two integers next to
by bluecalm 13y ago
You could easily use a + operator then.
I find the behavior surprising. I would expect a syntax error. You get a syntax error if you write two integers next to each other (separated by a space) or two of any other thing but somehow "a" "b" got converted to "ab".
If I've discovered it myself I would be tempted to fill a bug report. It goes against Python mantra:
"Explicit is better than implicit"
It seems someone thought along similar lines and took action: http://legacy.python.org/dev/peps/pep-3126/ http://legacy.python.org/dev/peps/pep-3126/ but the issue got rejected.
- famousactress 13y agoYeah, when I discovered this little "feature" I had a read through that. The folks that use this for blocks of multi-line text are very defensive about the practice. I do understand not wanting to break compatibility though, especially since finding instances of this is hard (which is another reason it shouldn't exist in the first place!). Oh well :)
- tripzilch 13y ago> The folks that use this for blocks of multi-line text are very defensive about the practice. odd, because we have triple-quotes for that, don't we?
- klibertp 13y agoNo, unfortunately we have not. With triple quotes you get a string with newlines and indentation in it. While you can not indent the following lines it looks ugly, and you can't do anything about newlines. Take a look at how F# handles the issue: http://stackoverflow.com/a/14599828 http://stackoverflow.com/a/14599828 And CoffeeScript: http://coffeescript.org/#strings http://coffeescript.org/#strings (apparently implemented relatively recently (https://github.com/jashkenas/coffeescript/issues/3229 https://github.com/jashkenas/coffeescript/issues/3229) and borrowed from LiveScript. There are other language which support multiline strings (Here docs) with indents stripped via means of syntax, like YAML (with |), Racket (which doesn't do dedenting, but being language it is it's very easy to add) and many shells (with <<-). Python doesn't have this feature, and parse-time string literals concatenation serves this purpose. Of course, you can do something like: foo = """bar indented at first and after newline """ textwrap.dedent(foo) (or use list literals with str.join, or use a regex, or many, many other thing), but you can do this in all languages. Languages with syntactic sugar for this make writing slightly-longer-but-not-too-long strings much easier and cheaper (only done once during parsing, no need for imports, etc.), and Python makes up for not having explicit way of doing this with implicit parse-time string literals concatenation.