4 ms·
I was going to comment something like "who would even use this?" and then I remembered that I have in fact used that feature :) It's a somewhat "nice" way to wr
by colpabar 5y ago
I was going to comment something like "who would even use this?" and then I remembered that I have in fact used that feature :) It's a somewhat "nice" way to write long strings and keep the code from getting too wide. I never did it inside an array, but I found breaking up a long string into smaller ones and wrapping them in parens without a comma was convenient, for things like error messages.
But that's just what comes with a hyper flexible language like python. You can do lots of things in lots of different ways, but you can also screw things up just as easily, and your IDE won't tell you because technically it's valid code.
- oaiey 5y agoI completely get that. That is a very nice feature for building DSL or libraries with special needs. But it makes the overall language very dangerous. Is this "operator" overloadable on each type in Python? And that scares me a lot. I think I have to reevaluate my position towards Python.
- housecarpenter 5y agoIt's not really an operator. It's part of the syntax of string literals. "foo" "bar" is an alternative way of writing the string literal "foobar". If foo is not a string literal, foo "bar" is invalid syntax.
- oaiey 5y agoOkay... So it is not a implicit operator. That is good. Some small reputation points are regained. Thanks.
- silisili 5y agoWhy not just use plusses? Or perhaps a join func, which would accomplish the same. I get the use case as you described it, but it just seems like minimal effort to accomplish and have some semblance of explicit/safety.
- dnautics 5y agoor if that's the use case, require the whitespace to include a \n or \r\n... It's not like python doesn't have significant whitespace already.
- ErikCorry 5y agoThat wouldn't fix most of the cases highighted by the tool in the article. So strange that Python has completely different syntax from C, but they chose to copy this obscure syntactic feature _even though they have the plus operator on strings_.
- TonyRobbins 5y ago
- shultays 5y agoYou could have the same behavior by enforcing + operation in between mylongstring = "hello" + "world" No idea if python's way of indentations allows this but sounds like it should
- ReleaseCandidat 5y agoNo, it doesn't: mylongstring = ("hello" + "world") or, without `+` mylongstring = ("hello" "world")
- fragmede 5y agoUse \ mylongstring = "hello " \ "world " \ "my " \ "name " \ "is"*
- BeetleB 5y agoThe use of \ is discouraged in Python. From PEP8: > The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
- contravariant 5y agoSee I knew Python just wants to be more like lisp.
- deleted 5y ago[deleted]
- BeetleB 5y agoHeh. I use it all the time the way you do and didn't realize this is alien to many developers (no one in my team every complained about it). It's common in some languages and used the way you use it. I looked in PEP8 and it seems they don't discuss this. I think it's a perfectly valid use case, but clearly there are two camps to this. If this is so contentious, I would recommend PEP8 be revised to either explicitly endorse it as a way to split long lines or to explicitly discourage it and recommend the + operator instead.