在 python 中,如果可能,如何调整黑色格式化程序?
In python, how to tweak Black formatter, if possible?
我知道 Black 是一个固执己见的格式化程序,但我喜欢它所做的一切,除了一件重要的事情。当我有一个带有多个参数的函数时,而不是像这样显示它:
def example_function(arg_1: str, arg_2: bool, arg_3: int = 0, arg_4: int = 1, arg_5: float = 0.0):
pass
为了便于阅读,我宁愿显示如下
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0
):
这可以用 Black 或其他格式化程序实现吗?我多次遇到这个问题,这让我考虑不使用 Black,要么用别的,要么什么都不用。有什么想法或意见吗?
在 https://black.now.sh, I found that your function was reformatted exactly like that when the line length was short enough(特别是 78 个字符)使用黑色游乐场时。
如果有一个特殊的配置选项来专门控制函数参数行的行长度,那可能会很好。但在我看来,Black 的无配置方法意味着无法通过任何更量身定制的选项来控制它。
这是因为 black
的默认行长度比您想要的要长 – 每行 88 个字符。
要减少行长度,您可以使用 --line-length
标志,如下所述:
https://github.com/psf/black/blob/master/docs/installation_and_usage.md#command-line-options
例如:
$ black --line-length 80 example.py
Black 在此处更详细地解释了 --line-length
设置:
https://github.com/psf/black/blob/master/docs/the_black_code_style.md#line-length
Line length
You probably noticed the peculiar default line length. Black defaults
to 88 characters per line, which happens to be 10% over 80. This
number was found to produce significantly shorter files than sticking
with 80 (the most popular), or even 79 (used by the standard library).
In general, 90-ish seems like the wise choice.
If you're paid by the line of code you write, you can pass
--line-length
with a lower number. Black will try to respect that. However, sometimes it won't be able to without breaking other rules.
In those rare cases, auto-formatted code will exceed your allotted
limit.
You can also increase it, but remember that people with sight
disabilities find it harder to work with line lengths exceeding 100
characters. It also adversely affects side-by-side diff review on
typical screen resolutions. Long lines also make it harder to present
code neatly in documentation or talk slides.
强调最后一段。
我建议只保留默认设置。 Black 的美妙之处在于它会为您选择,因此可以排除任何关于哪种方式是 “最佳”.
的争论
现在可以通过在最后一个参数中添加尾随逗号来实现。
在您的示例中,您应该这样写:
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0, # <-- Notice the trailing comma
):
我知道 Black 是一个固执己见的格式化程序,但我喜欢它所做的一切,除了一件重要的事情。当我有一个带有多个参数的函数时,而不是像这样显示它:
def example_function(arg_1: str, arg_2: bool, arg_3: int = 0, arg_4: int = 1, arg_5: float = 0.0):
pass
为了便于阅读,我宁愿显示如下
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0
):
这可以用 Black 或其他格式化程序实现吗?我多次遇到这个问题,这让我考虑不使用 Black,要么用别的,要么什么都不用。有什么想法或意见吗?
在 https://black.now.sh, I found that your function was reformatted exactly like that when the line length was short enough(特别是 78 个字符)使用黑色游乐场时。
如果有一个特殊的配置选项来专门控制函数参数行的行长度,那可能会很好。但在我看来,Black 的无配置方法意味着无法通过任何更量身定制的选项来控制它。
这是因为 black
的默认行长度比您想要的要长 – 每行 88 个字符。
要减少行长度,您可以使用 --line-length
标志,如下所述:
https://github.com/psf/black/blob/master/docs/installation_and_usage.md#command-line-options
例如:
$ black --line-length 80 example.py
Black 在此处更详细地解释了 --line-length
设置:
https://github.com/psf/black/blob/master/docs/the_black_code_style.md#line-length
Line length
You probably noticed the peculiar default line length. Black defaults to 88 characters per line, which happens to be 10% over 80. This number was found to produce significantly shorter files than sticking with 80 (the most popular), or even 79 (used by the standard library). In general, 90-ish seems like the wise choice.
If you're paid by the line of code you write, you can pass
--line-length
with a lower number. Black will try to respect that. However, sometimes it won't be able to without breaking other rules. In those rare cases, auto-formatted code will exceed your allotted limit.You can also increase it, but remember that people with sight disabilities find it harder to work with line lengths exceeding 100 characters. It also adversely affects side-by-side diff review on typical screen resolutions. Long lines also make it harder to present code neatly in documentation or talk slides.
强调最后一段。
我建议只保留默认设置。 Black 的美妙之处在于它会为您选择,因此可以排除任何关于哪种方式是 “最佳”.
的争论现在可以通过在最后一个参数中添加尾随逗号来实现。
在您的示例中,您应该这样写:
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0, # <-- Notice the trailing comma
):