Comments in continuation lines

You can't have comments and backslash for line continuation on the same line. You need to use some other strategy.

The most basic would be to adjust the comment text to place it e.g. before the relevant section. You could also document your intentions without comments at all by refactoring the code returning the context into a function or method with a descriptive name.


You cannot. Find some extracts from Python reference manual (3.4):

A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line.

A line ending in a backslash cannot carry a comment

A comment signifies the end of the logical line unless the implicit line joining rules are invoked

Implicit line joining : Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes

Implicitly continued lines can carry comments

So the reference manual explicitly disallows to add a comment in an explicit continuation line.