黑色格式化程序:如何在函数前后放置一个换行符而不是两个

black formatter: How can I place one newline before and after function instead of two

我正在使用 black formatter。我在函数定义之前有两个换行符,在函数定义之后有两个换行符。我想要在函数定义之前换行,在函数定义之后换行。

我可以使用黑色配置文件 black --config FILE 来实现吗?如果是的话,怎么样。

你不能。

By using it, you agree to cede control over minutiae of hand-formatting._

Current style:

It uses 2 lines to visually distinguish methods from empty lines in code structure. From the docs: Black will allow single empty lines inside functions, [...] It will also insert proper spacing before and after function definitions. It’s one line before and after inner functions and two lines before and after module-level functions and classes.

发件人:the docs of black.readthedocs.io

Customization:

Command line options Black has quite a few knobs these days, although Black is opinionated so style configuration options are deliberately limited and rarely added. You can list them by running black --help.

OP 在这里,我最终使用了 yapf

命令如下所示:

yapf --style={blank_lines_around_top_level_definition=1} file_name.py

对于多个参数:

yapf --style={based_on_style=pep8} --style={blank_lines_around_top_level_definition=1} file_name.py

在 vscode 中,您可以使用 yapf 样式,例如:

"python.formatting.yapfArgs": [
    "--style",
    "{based_on_style: pep8, blank_lines_around_top_level_definition: 1}"
],