如何使用 Google python 样式和 pylint 在 python 中设置长行样式?
How to style long lines in python using Google python style and pylint?
我正在尝试通过 运行 pylint 使用 google python 样式的 rc 文件来清理我的代码以进行分配。我只是想确认这是第一个打印行的正确样式,因为它看起来很奇怪,但是 google 样式 rcfile 显示它是正确的样式。我知道每行的长度不能超过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("")
我会这样格式化它:
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("")
任何澄清将不胜感激,谢谢
# 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)
(与Google Python Style Guide on indentation一致。)
此外,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)
你不应该在 print
中构建你的字符串。
当涉及到一个很长的消息时,分几个步骤来构建它。
s = "Your guess and score were: "
s += '_' * position
s += str(guess)
s += '_' * (len(word) - length - position)
s += " : "
s += str(score))
您可以使用 str.format
方法使它更干净一些。
参数将根据给定的名称替换花括号:
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)
这允许您将参数缩进为列表,以防它们的名称很长:
s = s.format(pad1=pad1,
pad2=pad2,
guess=guess,
score=score)
如果每个参数的定义足够短,可以发送给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)
如果你的字符串有很多要插值的值,你可以去掉变量名,但是,花括号将被相同顺序的参数替换:
s = "Your guess and score were: {}{}{} : {}"
s = s.format(pad1, guess, pad2, score)
正确,缩进取决于上一行的括号。但是可读性不仅仅是通过pylint,考虑一下:
print("Your guess and score were: {PAD1}{GUESS}{PAD2} : {SCORE}"
"".format(PAD1='_' * position,
GUESS=guess,
PAD2='_' * (len(word) - length - position),
SCORE=score))
(使用字符串连接可以更轻松地格式化较长的字符串。)
我正在尝试通过 运行 pylint 使用 google python 样式的 rc 文件来清理我的代码以进行分配。我只是想确认这是第一个打印行的正确样式,因为它看起来很奇怪,但是 google 样式 rcfile 显示它是正确的样式。我知道每行的长度不能超过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("")
我会这样格式化它:
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("")
任何澄清将不胜感激,谢谢
# 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)
(与Google Python Style Guide on indentation一致。)
此外,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)
你不应该在 print
中构建你的字符串。
当涉及到一个很长的消息时,分几个步骤来构建它。
s = "Your guess and score were: "
s += '_' * position
s += str(guess)
s += '_' * (len(word) - length - position)
s += " : "
s += str(score))
您可以使用 str.format
方法使它更干净一些。
参数将根据给定的名称替换花括号:
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)
这允许您将参数缩进为列表,以防它们的名称很长:
s = s.format(pad1=pad1,
pad2=pad2,
guess=guess,
score=score)
如果每个参数的定义足够短,可以发送给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)
如果你的字符串有很多要插值的值,你可以去掉变量名,但是,花括号将被相同顺序的参数替换:
s = "Your guess and score were: {}{}{} : {}"
s = s.format(pad1, guess, pad2, score)
正确,缩进取决于上一行的括号。但是可读性不仅仅是通过pylint,考虑一下:
print("Your guess and score were: {PAD1}{GUESS}{PAD2} : {SCORE}"
"".format(PAD1='_' * position,
GUESS=guess,
PAD2='_' * (len(word) - length - position),
SCORE=score))
(使用字符串连接可以更轻松地格式化较长的字符串。)