26

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.

6
  • 9
    Do you know you could just wrap the whole expression in parentheses to turn on automatic line continuation? Plus, comments work inside that. Jun 9, 2021 at 21:46
  • 2
    I did not! Thank you. Jun 9, 2021 at 21:47
  • 2
    Note that method chaining is "fluent", not reactive.
    – jonrsharpe
    Jun 9, 2021 at 21:47
  • @jonrsharpe thank you, updated. I was first introduced to this style with react, which I suppose does not even require it although it benefits significantly from it. Jun 9, 2021 at 21:54
  • 1
    Related: Is there a way to put comments in multiline code? [I just edited its title btw.]
    – wjandrea
    Jun 9, 2021 at 21:59

2 Answers 2

42

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()
)
2
  • I did not know that dots can be on different lines. Cool
    – TUSqasi
    Jun 16, 2021 at 7:04
  • 1
    @TUSqasi It's only because the lines are all contained within a set of parentheses. Outside of some kind of grouper, you can't, sadly. Luckily, as this answer is implying, you can use parentheses for this type of thing quite often. Jun 16, 2021 at 15:51
10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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