Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用长字符串将字符宽度保持在80字符以下?_Python_Coding Style_Readability_Code Readability - Fatal编程技术网

Python 如何使用长字符串将字符宽度保持在80字符以下?

Python 如何使用长字符串将字符宽度保持在80字符以下?,python,coding-style,readability,code-readability,Python,Coding Style,Readability,Code Readability,我现在正试图将我的代码保持在80个字符或更少,因为我认为它在很大程度上看起来更美观。不过,有时候,如果我不得不在奇怪的地方换行,代码最终会变得更糟糕 有一件事我还没有弄清楚如何很好地处理长字符串。例如: #0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx def foo(): if conditional(): logger.info

我现在正试图将我的代码保持在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……..8xxxxxxx9xxxxxx
def foo():
如果条件():

logger.info(“您可以将字符串拆分为两个:

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

在字节编译器的窥视孔优化中,Python会在编译时折叠常量上的二进制操作(因此
+
*
-
等)。因此对于某些字符串串联,编译器也可以用串联结果替换常量的
+
字符串串联。有关序列,请参阅(包括字符串)仅当结果限制为20项(字符)时才应用此优化或更少。

@nightcracker:编译器合并字符串。使用<代码>+
将连接移动到运行时。@nightcracker:添加了证据。抱歉,但您错了。使用<代码>+有两个字符串常量,它们在运行时添加。@MartijnPieters反证据:它们同样快。@nightcracker:看起来是这样的有些字符串编译器优化了字符串连接。在这种情况下,编译时连接不会发生。我在要点中添加了一个计数器示例。@MartijnPieters啊,好的。无论哪种方式,性能参数在我看来都是无关紧要的,除非我们在这里讨论性能瓶颈。但我强烈怀疑这一点他的日志记录条目是性能瓶颈。
#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

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