Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't comments appear after a line continuation character?

Tags:

python

Why does Python not allow a comment after a line continuation "\" character, is it a technical or stylistic requirement?

According to the docs (2.1.5):

A line ending in a backslash cannot carry a comment.

and (2.1.3):

A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Comments are ignored by the syntax.

PEP 8 does discourage inline comments so I can see how this may stylistically be "unpythonic." I could also see how the "\" could be ambiguous if it allowed comments (should the interpreter ignore all subsequent tokens or just comments?) but I think a distinction could easily be made.

Coming from Javascript and accustomed to the fluid interface style, I prefer writing chains like the following instead of reassignment:

dataset = tf.data.Dataset.from_tensor_slices(data)\
        .interleave(special_sauce_fn)\
        .shuffle(shuffle_buffer_size)\
        .batch(batch_size)\
        .prefetch()\
        .cache()

Instead of

dataset = dataset.interleave(special_sauce_fn)
dataset = dataset.shuffle(shuffle_buffer_size)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch()
dataset = dataset.cache()

While this example is rather self-explanatory, at times I feel like a contextual comment could enhance readability of the code.

like image 550
BobtheMagicMoose Avatar asked Jun 09 '21 21:06

BobtheMagicMoose


People also ask

What is the line continuation character and what does it do?

The line continuation character in IPL and JavaScript is the backslash (\). You use this character to indicate that the code on a subsequent line is a continuation of the current statement. The line continuation character helps you format your policies so that they are easier to read and maintain.

How do you continue the comment on the next line in Python?

You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line.

What is line continuation character in C?

The backslash character ('') is the line continuation character. It must be the last character on a line. The preprocessor joins lines ending with a line continuation character into a single line (for purposes of preprocessing and compilation).

Which character is used as the line continuation character?

Use the line-continuation character, which is an underscore ( _ ), at the point at which you want the line to break.


2 Answers

Generally, line-continuation characters are discouraged. Instead, use parentheses:

dataset = (
      tf.data.Dataset.from_tensor_slices(data)
        .interleave(special_sauce_fn) #  comment to your
        .shuffle(shuffle_buffer_size) #  heart's delight
        .batch(batch_size)
        .prefetch()
        .cache()
)
like image 68
juanpa.arrivillaga Avatar answered Oct 20 '22 00:10

juanpa.arrivillaga


For the same reason you can't have whitespace after the backslash. It simplifies the parsing, as it can simply remove any backslash-newline pairs before doing any more parsing. If there were spaces or comments after the backslash, there's no backslash-newline sequence to remove.

like image 29
Barmar Avatar answered Oct 20 '22 00:10

Barmar