如何清洁地保持低于80字符宽度与长字符串? [英] How to cleanly keep below 80-char width with long strings?

查看:160
本文介绍了如何清洁地保持低于80字符宽度与长字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的代码保持在80字符或更少,因为我认为它看起来更美观,大部分。有时候,如果我不得不在奇怪的地方换行,代码最终看起来更糟。



有一件事我还没有想出如何处理非常好,但是很长的字符串。例如:

 #0 ......... 1 ........ 2 .. ...... 3 ....... 4 ......... 5 ......... 6 ......... 7 .... ..... 8xxxxxxxxx9xxxxxx 
def foo():
如果条件():
logger.info(< Conditional's meaning>发生,所以我们不设置界面。 )
return

#.....

结束了!将它放在下一行不会有帮助:

 #0 ......... 1 .. ...... 2 ....... 3 ....... 4 ......... 5 ......... 6 ..... .... 7 ......... 8xxxxxxxxx9xxxxxx 
def foo():
如果条件():
logger.info(
< Conditional's meaning> ;发生了,所以我们不设置界面。)
return

#.....

我可以使用换行符,但看起来很可怕:

 #0。 ........ 1 ....... 2 ....... 3 ....... 4 ......... 5 .... ..... 6 ......... 7 ......... 8 
def foo():
如果条件():
logger。 info(
< Conditional的意思>发生了,所以我们没有设置\
上的界面)
return

#.... 。

该怎么办?缩短字符串是一个选项,但我不想让我的消息的可读性受到某些东西的影响,因为代码在那一刻有多少缩进级别。

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

  def foo 
如果条件():
logger.info(< Conditional的含义>发生,所以我们不是
设置界面。)

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

 > ;> def foo():
...如果条件():
... logger.info(< Conditional的含义>发生,所以我们不是
...设置界面)
...
>>> import dis
>>>> dis.dis(foo)
2 0 LOAD_GLOBAL 0(条件)
3 CALL_FUNCTION 0
6 POP_JUMP_IF_FALSE 25

3 9 LOAD_GLOBAL 1(logger)
12 LOAD_ATTR 2(信息)
15 LOAD_CONST 1(< Conditional的含义>发生了,所以我们不设置接口。)
18 CALL_FUNCTION 1
21 POP_TOP
22 JUMP_FORWARD 0(to 25)
>> 25 LOAD_CONST 0(无)
28 RETURN_VALUE

请注意 LOAD_CONST 第3行,函数的字节码包含一个字符串,已连接。



a + 到表达式,创建两个单独的常量:

  ;>> def foo():
...如果条件():
... logger.info(< Conditional的含义>发生,所以我们不是+
... 设置界面。)
...
>>> dis.dis(foo)
2 0 LOAD_GLOBAL 0(条件)
3 CALL_FUNCTION 0
6 POP_JUMP_IF_FALSE 29

3 9 LOAD_GLOBAL 1(logger)
12 LOAD_ATTR 2(信息)
15 LOAD_CONST 1(< Conditional的含义>发生,所以我们不是)

4 18 LOAD_CONST 2('设置界面'
21 BINARY_ADD
22 CALL_FUNCTION 1
25 POP_TOP
26 JUMP_FORWARD 0(至29)
>> 29 LOAD_CONST 0(None)
32 RETURN_VALUE

Python在编译时会对常数进行二进制运算时间(因此 + * - 等) ,在字节编译器的窥视孔优化。因此,对于某些字符串连接,编译器 也可以用连接的结果替换 + 字符串连接的常量。请参见 peephole.c ,对于序列(包括字符串),此优化仅适用于结果限制为20个项目(字符)或更少。


I'm attempting to keep my code to 80 chars or less nowadays as I think it looks more aesthetically pleasing, for the most part. Sometimes, though, the code ends up looking worse if I have to put line breaks in weird places.

One thing I haven't figured out how to handle very nicely yet is long strings. For example:

#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

    #.....

It's over! Putting it on the next line won't help either:

#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

    #.....

I could use line breaks but that looks awful:

#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

    #.....

What to do? Shortening the string is one option but I don't want the readability of my messages to be affected by something as arbitrary as how many indentation levels the code happened to have at that point.

解决方案

You can split the string into two:

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

Multiple consecutive strings within the same expression are automatically concatenated into one, at compile time:

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

Note the LOAD_CONST for line 3, the bytecode for the function contains one string, already concatenated.

If you were to add a + to the expression, two separate constants are created:

>>> 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 does fold binary operations on constants at compile time (so +, *, - etc.), in the peephole optimizations for the byte compiler. So for certain string concatenations the compiler may also replace + string concatenation of constants with the concatenated result. See peephole.c, for sequences (including strings) this optimization is only applied if the result is limited to 20 items (characters) or fewer.

这篇关于如何清洁地保持低于80字符宽度与长字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆