22

我现在正试图将我的代码保持在 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

    #.....

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

#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

    #.....

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

#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

    #.....

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

4

1 回答 1

38

您可以将字符串分成两部分:

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

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

>>> 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        

注意第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        

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

于 2013-03-27T16:28:54.903 回答