如何使用Google python样式和pylint在python中设置长行样式?
我试图通过使用googlepython风格的rc文件在代码上运行pylint来清理分配的代码。我只是想确认这是第一行打印的正确样式,因为它看起来很奇怪,但是google样式的rcfile显示它是正确的样式。我知道每行的长度不能超过80个字符如何使用Google python样式和pylint在python中设置长行样式?,python,python-3.x,pylint,pylintrc,Python,Python 3.x,Pylint,Pylintrc,我试图通过使用googlepython风格的rc文件在代码上运行pylint来清理分配的代码。我只是想确认这是第一行打印的正确样式,因为它看起来很奇怪,但是google样式的rcfile显示它是正确的样式。我知道每行的长度不能超过80个字符 for position, length in STEPS: guess = prompt_guess(position, length) score = compute_score(guess, position, word) to
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("")
如有任何澄清,将不胜感激,请参见:
(符合。)
还有:
您不应该在
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)
如果每个参数的定义足够短,可以将其发送到格式
方法:
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))
(使用字符串连接可以更容易地格式化较长的字符串。)将多个参数传递给
print()
?您不必相互添加字符串。我希望猜测和评分在同一行上,尽管在这样的输出中,您的猜测和评分是:at_uuuuuuuuu0print(“a”、“b”、“c”)
->abc
在print
调用中进行字符串转换和+
串联是一种反模式。让print
为您执行转换和连接:它更干净、更高效。如果需要更多地控制输出的间距,请使用.format
(或Python 3.6中的f字符串)。顺便说一句,format
方法(或format
函数)也可以进行填充。这非常有用,非常感谢。我已经编辑了上面的代码。@S.Mac不客气。尊重80个字符的限制是一件让人头疼的事,不是吗:)@S.Mac这是一个很好的计划,愿意改进自己的编码风格,因为大多数情况下,它决定了代码的好坏。无论如何,你真的应该看看,它将引导你在这条轨道上。
s = "Your guess and score were: {}{}{} : {}"
s = s.format(pad1, guess, pad2, score)
print("Your guess and score were: {PAD1}{GUESS}{PAD2} : {SCORE}"
"".format(PAD1='_' * position,
GUESS=guess,
PAD2='_' * (len(word) - length - position),
SCORE=score))