Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to know recommendations about Implicit Line Joining versus Explicit Line Joining in Python.

In particular, do you favor one form over the other? What do you recommend as the general default? What criteria do you have for choosing one over the other, and if you do have a preference of one, when do you make exceptions for the other?

I have an answer in mind for this question that reflects my own biases, but before I post my own answer I would like to know what others think... and if you can have a better set of criteria than what I have in mind, then I'll certainly accept your answer over my own.

Some of the recommendations may be generalized to this choice in other programming languages, but my own bias is somewhat stronger in Python due to some language-specific features, so I'd like to know both the general and the Python-centric reasoning you may have on this topic.

For some background, the discussion happened around a particular question on stackoverflow, but I thought it was more appropriate to move the discussion over to here as a question to avoid cluttering up the answer on SO with this tangent since it has veered off-topic from the original question. You can look at that question and its answers for the example code snippets that got the discussion going.

Here is a simplified example:

join_type = "explicit"
a = "%s line joining" \
    % (join_type)
# versus
join_type = "implicit"
b = ("%s line joining"
     % (join_type))
share|improve this question

migrated from codereview.stackexchange.com Dec 28 '11 at 22:25

This question came from our site for peer programmer code reviews.

    
Best practice questions are off topic for code review. I've migrated your question to a better venue. –  Winston Ewert Dec 28 '11 at 22:28
1  
@WinstonEwert before posting I took a good look at the CodeReview FAQ and the Programmers FAQ, and I chose CodeReview because it explicit says that the kinds of questions to ask there include "Best practices and design pattern usage in your code". I included a simplified version of the code in question, so how is this off-topic? –  aculich Dec 29 '11 at 13:49
    
@WinstonEwert I have posted a question in Meta about clarifying the CodeReview FAQ if you care to comment on this over there. –  aculich Dec 29 '11 at 14:26

3 Answers 3

Presently, I would prefer

join_type = "kiding"
a = "%s line joining" % (join_type)

B-))

.

I tend to prefer Explicit Lines Joining because I don't like the clutter of parens at the end of expressions.
But I like Implicit Lines Joining to reduce the width occupied by the writing of a string.
Then in some cases, I am embarassed to not mix the two ways

share|improve this answer
    
All kidding aside, I don't like explicit joining because it requires more typing and it's difficult to keep all the backslashes neatly lined up when the code is edited. –  martineau Jan 29 '13 at 16:07

There is a coding style document called PEP8. It recommends against the usage of \<NL> wherever parentheses can be used.

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. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

Full text: http://www.python.org/dev/peps/pep-0008/ (section Code Layout)

It is not compulsory but it defines acceptable good practices which are especially useful if you have multiple Python committers on your team.

share|improve this answer

I tend to use implicit line joining because I find it more readable and the support from editors is usually better with regard to indentation and highlighting of the whole expression thanks to parenthesis matching.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.