简体   繁体   English

如何使用Google python样式和pylint在python中设置长行样式?

[英]How to style long lines in python using Google python style and pylint?

I am trying to clean up my code for an assignment by running pylint over it with the google python style rc file. 我正在尝试通过使用google python样式rc文件在其上运行pylint来清理作业代码。 I just want to confirm that this is the correct style for the the first print line, as it look pretty weird, but the google style rcfile is showing that it is the correct style. 我只想确认这是第一个打印行的正确样式,因为它看起来很奇怪,但是google样式rcfile显示它是正确的样式。 I know that the length of each line mustn't exceed 80 characters 我知道每行的长度不能超过80个字符

for position, length in STEPS:
    guess = prompt_guess(position, length)
    score = compute_score(guess, position, word)
    total = + total + score
    print("Your guess and score were: " + ('_' * position + str(guess) +
                                           ('_' * (len(word) - length -
                                                   position))) + " : " +
          str(score))
    print("")

I would've formatted it like this: 我会像这样格式化它:

for position, length in STEPS:
    guess = prompt_guess(position, length)
    score = compute_score(guess, position, word)
    total = + total + score
    print("Your guess and score were: " + ('_' * position + str(guess) +
         ('_' * (len(word) - length -position))) + " : " + str(score))
    print("")

Any clarification would be appreciated, thanks 任何澄清将不胜感激,谢谢

You shouldn't build your string inside print . 您不应该在print构建字符串。 When it comes to a very long message, take several steps to build it. 当涉及到很长的消息时,请采取几个步骤来构建它。

s = "Your guess and score were: "
s += '_' * position
s += str(guess)
s += '_' * (len(word) - length - position)
s += " : "
s += str(score))

You can make it a bit cleaner by using the str.format method. 您可以使用str.format方法使其更加干净。 The parameters will replace the curly braces, according to the names given: 这些参数将根据给定的名称替换花括号:

pad1 = '_' * position
pad2 = '_' * (len(word) - length - position)
s = "Your guess and score were: {pad1}{guess}{pad2} : {score}"
s = s.format(pad1=pad1, pad2=pad2, guess=guess, score=score)

This allows you to indent the parameters as a listing, in case their names are long: 这可以让您将参数缩进清单中,以防名称长:

s = s.format(pad1=pad1,
             pad2=pad2,
             guess=guess,
             score=score)

If the definition of each parameter is short enough, you can send it to the format method: 如果每个参数的定义都足够短,则可以将其发送给format方法:

s = "Your guess and score were: {pad1}{guess}{pad2} : {score}"
s = s.format(pad1='_' * position,
             pad2='_' * (len(word) - length - position),
             guess=guess,
             score=score)

If your string has a lot of values to be interpolated, you can get rid of the variable names, but then, the curly braces will be replaced by the parameters in the same order: 如果您的字符串有很多要插值的值,则可以删除变量名,但是花括号将被参数替换为相同的顺序:

s = "Your guess and score were: {}{}{} : {}"
s = s.format(pad1, guess, pad2, score)

See PEP-8 on indentation : 有关缩进,请参阅PEP-8

# YES: Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# NO: Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)

(Consistent with Google Python Style Guide on indentation .) (与Google Python样式指南中的缩进一致 。)

Also, should a line break before or after a binary operator? 另外, 换行符应该在二进制运算符之前还是之后? :

# NO: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

# YES: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

Correct, indentation depends on previous line's parentheses. 正确,缩进取决于前一行的括号。 But readability is more than just passing pylint, consider: 但是,可读性不仅仅是传递pylint,请考虑:

print("Your guess and score were: {PAD1}{GUESS}{PAD2} : {SCORE}"
      "".format(PAD1='_' * position,
                GUESS=guess,
                PAD2='_' * (len(word) - length - position),
                SCORE=score))

(Use of string concatenation makes for easier formatting of longer strings.) (使用字符串串联可以更轻松地格式化较长的字符串。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM