多行字符串的缩进
我有一个使用 cmd Python 模块的脚本。 cmd 模块使用三重引号多行字符串作为帮助文本。 像这样
def x(self, strags = None):
"""class
help text here
and some more help text here"""
运行脚本时,命令“help x”将打印字符串。 但是,它也会在最后两行前面打印换行符。 我可以通过不缩进这些行来克服这个问题,但这会使我的代码变得丑陋{y,ier}。
如何克服这个缩进问题? 专业 Python 程序员如何处理这个问题?
I have a script that uses the cmd Python module. The cmd module uses a triple quoted multiline string as it's help text. Something like this
def x(self, strags = None):
"""class
help text here
and some more help text here"""
When running the script, the command 'help x' will print the string. It will, however, print the newlines in front of the last two lines as well. I can overcome this by not indenting these lines, but that'll make my code ugl{y,ier}.
How to overcome this indenting problem? How do the pro Python coders handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我尝试遵循 PEP 8 ,它将读者引向 PEP 257 用于文档字符串约定。 它有一个关于多行文档字符串的完整部分。
Personally I try to follow PEP 8 which refers the reader to PEP 257 for Docstring Conventions. It has an entire section on multi-line docstrings.
我会通过一致的缩进来处理它,如下所示:
当然,它需要多两行,但它也通过使文档注释非常突出来注入清晰度(在我看来)。
I'd handle it by having consistent indents, like this:
Sure, it takes two lines more, but it also injects clarity (in my opinion) by making the doc comment stand out quite well.