如何注释多行语句的一行

jav*_*dba 3 python

如下面的代码所示,该groupby行应该被注释掉:

lines = fileinput.input(fin) \
        | take(300) \
        | where(lambda x: not x.strip().endswith(',,,,,')) \
        \ # | groupby(lambda x: x[42]) 
        | teeFile(fout,100)
Run Code Online (Sandbox Code Playgroud)

但是上面的语法 - 以及它的几个变体 - 不起作用:

 \ # | groupby(lambda x: x[42])
                                                                                                                                                                 ^
SyntaxError: unexpected character after line continuation character
Run Code Online (Sandbox Code Playgroud)

已尝试的另一种变化:

    # | groupby(lambda x: x[42]) \ 
Run Code Online (Sandbox Code Playgroud)

有没有办法评论一个需要延续字符的较长陈述的一部分?或者我们只是运气不好 - 就像python无法(/不愿意)支持内联评论一样?

我在 2.7

更新 此处是对代码段的一个小更新,使其完全自包含.

import sys, pipe, fileinput ; from pipe import *;
lines = fileinput.input(fin) \
        | take(300) \
        | where(lambda x: not x.strip().endswith(',,,,,,,,')) \
        # | groupby(lambda x: x[42]) \
        | tee
Run Code Online (Sandbox Code Playgroud)

它现在只包括一个导入.我在ipythonvs中遇到了不同的错误intellij:

ipython :

File "<ipython-input-2-60c5dbee382d>", line 3
    | tee
    ^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)

intellij :

 File "<ipython-input-30-1f7b64578a1f>", line 16
    lines = fileinput.input(fin)         | take(300)         | where(lambda x: not x.strip().endswith(',,,,,,,,,,,,,,,,,,,,,'))   \ # | groupby(lambda x: x[42])
                                                                                                                                                                 ^
SyntaxError: unexpected character after line continuation character
Run Code Online (Sandbox Code Playgroud)

use*_*ica 6

对括号使用隐式行继续:

lines = (fileinput.input(fin)
        | take(300)
        | where(lambda x: not x.strip().endswith(',,,,,'))
#       | groupby(lambda x: x[42]) 
        | teeFile(fout,100))
Run Code Online (Sandbox Code Playgroud)

在未闭合的括号(,括号[或大括号内{,Python将自动执行行继续,甚至跨越带注释的行.使用反斜杠进行行连接的规则不允许在带注释的行上继续行.

  • @ChristianDean:不可能。[显式行连接规则](https://docs.python.org/2.7/reference/lexical_analysis.html#explicit-line-joining)显式排除注释;只有隐式行连接才能通过注释继续逻辑行。 (2认同)