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

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

    #.....

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

#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

    #.....

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

最佳答案

你可以把字符串一分为二:

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

同一表达式中的多个连续字符串自动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        

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

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

>>> 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 确实在编译时折叠常量上的二进制操作(如 +*- 等),在窥视孔优化中字节编译器。因此,对于某些字符串连接,编译器可能 也将 + 字符串连接的常量替换为连接的结果。见 peephole.c ,对于序列(包括字符串),仅当结果限制为 20 个项目(字符)或更少时才会应用此优化。

关于python - 如何用长字符串干净地保持低于 80 字符的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15664101/

相关文章:

python - 如何在第一次调用 paramiko 命令时设置密码?

coding-style - 检查字典中的值是否已定义/长度为零的最 Pythonic 方式

javascript - 我们应该在 JavaScript 编码中使用长名还是短名?

python - 使用 Python 3 将一个文件的内容复制到另一个文件中

python - Django 帮助 : AttributeError: 'module' object has no attribute 'Charfield'

python - 如何在 Python 中序列化 CObject?

coding-style - 如何为不同的 clang-format 版本维护 .clang-format 文件?

performance - 功能分离还是不分离?那就是

c# - 第一次访问属性时的编码风格和支持字段成员为空

c++ - 模板编程的可维护性建议和最佳实践