如何用长串干净地保持80-字符宽度?

Cla*_*diu 22 python coding-style readability code-readability

我现在试图将我的代码保持在80或更少,因为我认为它在大多数情况下看起来更美观.但有时,如果我必须在奇怪的地方放置换行符,代码最终会变得更糟.

有一件事我还没弄清楚如何处理非常好的是长串.例如:

#0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx
def foo():
    if conditional():
        logger.info("<Conditional's meaning> happened, so we're not setting up the interface.")
        return

    #.....
Run Code Online (Sandbox Code Playgroud)

结束了!将它放在下一行也无济于事:

#0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx
def foo():
    if conditional():
        logger.info(
            "<Conditional's meaning> happened, so we're not setting up the interface.")
        return

    #.....
Run Code Online (Sandbox Code Playgroud)

我可以使用换行但看起来很糟糕:

#0.........1........2........3........4.........5.........6.........7.........8
def foo():
    if conditional():
        logger.info(
            "<Conditional's meaning> happened, so we're not setting \
up the interface.")
        return

    #.....
Run Code Online (Sandbox Code Playgroud)

该怎么办?缩短字符串是一种选择,但我不希望我的消息的可读性受到与该代码恰好有多少缩进级别一样任意的影响.

Mar*_*ers 38

您可以将字符串拆分为两个:

def foo():
    if conditional():
        logger.info("<Conditional's meaning> happened, so we're not "
                    "setting up the interface.")
Run Code Online (Sandbox Code Playgroud)

在编译时,同一表达式中的多个连续字符串会自动连接成一个:

>>> def foo():
...     if conditional():
...         logger.info("<Conditional's meaning> happened, so we're not "
...                     "setting up the interface.")
... 
>>> import dis
>>> dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (conditional)
              3 CALL_FUNCTION            0
              6 POP_JUMP_IF_FALSE       25

  3           9 LOAD_GLOBAL              1 (logger)
             12 LOAD_ATTR                2 (info)
             15 LOAD_CONST               1 ("<Conditional's meaning> happened, so we're not setting up the interface.")
             18 CALL_FUNCTION            1
             21 POP_TOP             
             22 JUMP_FORWARD             0 (to 25)
        >>   25 LOAD_CONST               0 (None)
             28 RETURN_VALUE        
Run Code Online (Sandbox Code Playgroud)

注意第LOAD_CONST3行,函数的字节码包含一个已连接的字符串.

如果+要在表达式中添加a ,则会创建两个单独的常量:

>>> def foo():
...     if conditional():
...         logger.info("<Conditional's meaning> happened, so we're not " + 
...                     "setting up the interface.")
... 
>>> dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (conditional)
              3 CALL_FUNCTION            0
              6 POP_JUMP_IF_FALSE       29

  3           9 LOAD_GLOBAL              1 (logger)
             12 LOAD_ATTR                2 (info)
             15 LOAD_CONST               1 ("<Conditional's meaning> happened, so we're not ")

  4          18 LOAD_CONST               2 ('setting up the interface.')
             21 BINARY_ADD          
             22 CALL_FUNCTION            1
             25 POP_TOP             
             26 JUMP_FORWARD             0 (to 29)
        >>   29 LOAD_CONST               0 (None)
             32 RETURN_VALUE        
Run Code Online (Sandbox Code Playgroud)

Python做在编译时(因此折上常量二进制操作+,*,-等等),在窥孔优化的字节编译器.因此,对于某些字符串连接,编译器可以用+连接结果替换常量的字符串连接.请参阅peephole.c,对于序列(包括字符串),仅当结果限制为20个项目(字符)或更少时才应用此优化.

  • @nightcracker:*编译器*合并字符串.用`+`将连接移动到运行时. (4认同)