代码之家  ›  专栏  ›  技术社区  ›  user225312

用python包长线[重复]

  •  178
  • user225312  · 技术社区  · 16 年前

    如何在不牺牲缩进的情况下用Python包装长线?

    例如:

    def fun():
        print '{0} Here is a really long sentence with {1}'.format(3, 5)
    

    假设超过了建议的79个字符的限制。我读它的方式是,如何缩进它:

    def fun():
        print '{0} Here is a really long \
    sentence with {1}'.format(3, 5)
    

    但是,使用这种方法,连续行的缩进与 fun() . 这看起来有点难看。如果有人要浏览我的代码,那么由于这个原因出现不均匀的缩进看起来会很糟糕。 print 语句。

    如何在不牺牲代码可读性的情况下有效地缩进这样的行?

    7 回复  |  直到 7 年前
        1
  •  238
  •   0 _ Edward Ned Harvey    9 年前
    def fun():
        print(('{0} Here is a really long '
               'sentence with {1}').format(3, 5))
    

    与C中一样,在编译时连接相邻的字符串文本。 http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation 是获取更多信息的好地方。

        2
  •  66
  •   0 _ Edward Ned Harvey    11 年前

    有两种方法没有在上面提到,但都以符合的方式解决了问题 PEP 8 和 让你更好地利用你的空间。他们是:

    msg = (
        'This message is so long, that it requires '
        'more than {x} lines.{sep}'
        'and you may want to add more.').format(
            x=x, sep=2*'\n')
    print(msg)
    

    注意圆括号是如何使用的,它允许我们不在纯字符串之间添加加号,并且在多行上传播结果,而不需要显式的行继续符''(难看且混乱)。 其优点与下面描述的相同,区别在于您可以在任何地方进行。 与前面的替代方案相比,在检查代码时,它的视觉效果更好,因为它概述了 msg 清楚(与 msg += 每行一个,这需要一个额外的思考步骤来推断这些行添加到同一个字符串中——如果你输入了错别字,忘记了 + 在一条随机线上?).

    对于这种方法,很多时候我们必须使用迭代体中的迭代和检查来构建一个字符串,因此在函数调用中添加它的片段(如后面所示)不是一个选项。

    一个很好的选择是:

    msg = 'This message is so long, that it requires '
    msg += 'many lines to write, one reason for that\n'
    msg += 'is that it contains numbers, like this '
    msg += 'one: ' + str(x) +', which take up more space\n'
    msg += 'to insert. Note how newlines are also included '
    msg += 'and can be better presented in the code itself.'
    print(msg)
    

    尽管第一个更好。

    另一种方法与前面的方法类似,尽管它在 print . 这样做的原因是为了在左侧获得空间,否则 print( 它会把你推向右边。缩进的消耗量是由包含消息的其余行继承的,因为根据PEP 8,它们必须与的左括号对齐。 打印 在他们上面。所以,如果你的信息已经很长了,那么它就不得不在更多的行上传播。

    对比度:

    raise TypeError('aaaaaaaaaaaaaaaa' +
                    'aaaaaaaaaaaaaaaa' +
                    'aaaaaaaaaaaaaaaa')
    

    有了这个(建议在这里):

    raise TypeError(
        'aaaaaaaaaaaaaaaaaaaaaaaa' +
        'aaaaaaaaaaaaaaaaaaaaaaaa')
    

    线间距减小。当然,最后一种方法不太适用于 打印 因为这是一个短电话。但它确实适用于例外情况。

    您可以使用的变体是:

    raise TypeError((
        'aaaaaaaaaaaaaaaaaaaaaaaa'
        'aaaaaaaaaaaaaaaaaaaaaaaa'
        'aaaaa {x} aaaaa').format(x=x))
    

    注意纯字符串之间不需要有加号。此外,压痕引导读者的眼睛,左下方没有杂散的括号。替换的内容可读性很强。特别是,这种方法使得编写生成代码或数学公式的代码成为一项非常愉快的任务。

        3
  •  21
  •   SilentGhost    16 年前

    如果缩进不重要,可以使用以下代码:

    >>> def fun():
            return ('{0} Here is a really long'
            ' sentence with {1}').format(3, 5)
    

    您只需要将字符串括在括号中。

        4
  •  18
  •   Chris B.    16 年前

    可以使用以下事实:python连接彼此相邻的字符串文本:

    >>> def fun():
    ...     print '{0} Here is a really long ' \
    ...           'sentence with {1}'.format(3, 5)
    
        5
  •  7
  •   Mark Byers    16 年前

    我可能会将long语句拆分为多个较短的语句,以便程序逻辑与long字符串的定义分开:

    >>> def fun():
    ...     format_string = '{0} Here is a really long ' \
    ...                     'sentence with {1}'
    ...     print format_string.format(3, 5)
    

    如果字符串太长,并且您选择了一个短变量名,那么通过这样做,您甚至可以避免拆分字符串:

    >>> def fun():
    ...     s = '{0} Here is a really long sentence with {1}'
    ...     print s.format(3, 5)
    
        6
  •  3
  •   Taylor D. Edmiston    9 年前

    我很惊讶没有人提到上面的隐式风格。我的偏好是使用parens将字符串包装起来,同时以视觉方式排列字符串。就我个人而言,我认为这看起来比在一个有标签的新行上启动字符串的开头更干净、更紧凑。

    注意,这些参数不是方法调用的一部分,它们只是 implicit string literal concatenation .

    Python 2:

    def fun():
        print ('{0} Here is a really '
               'long sentence with {1}').format(3, 5)
    

    python 3(带有打印函数parens):

    def fun():
        print(('{0} Here is a really '
               'long sentence with {1}').format(3, 5))
    

    就个人而言,我认为将连接的长字符串文字与打印的字符串文字分开是最干净的:

    def fun():
        s = ('{0} Here is a really '
             'long sentence with {1}').format(3, 5)
        print(s)
    
        7
  •  0
  •   Nabin    7 年前

    如果是长字符串(例如用于SQL查询),请使用三重代码

    '''
    hello this is me 
    '''