Thursday, January 20, 2011

Python note: breaking a long statement to add end of line comments

There is a very simple tip for breaking a long line in order to add end comments. Python doesn't recognize arbitrary broken lines, but it allows lines to be broken if the line is inside a bracket so it is unambiguous. The following code

if condition1 and # comment1
condition2: # comment2

would not work because a line is broken in the if statement, however,

if (condition1 and #comment1
condition2): # comment2

would fix the problem and maintain neat code.

No comments: