具有多行值的PEP8多行字典 [英] PEP8 multi-line dict with multi-line value

查看:51
本文介绍了具有多行值的PEP8多行字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Black用于Python,它符合PEP8 .它从两行长值字符串的第二行中删除缩进:

I use Black for Python, which conforms to PEP8. It removes the indentation from the second line of a two line long value string:

mydict = {
    'key0': 'value0',
    'key1': 'long-two-lines-string-value1-does-not-fit-in-one-line-has-to-continue'
            'value1'
}

收件人:

mydict = {
    'key0': 'value0',
    'key1': 'long-two-lines-string-value1-does-not-fit-in-one-line-has-to-continue'
    'value1',
}

一位同事对这一变化提出了质疑,我想知道是否有任何资源/参考可以用来备份布莱克决定格式化代码的决定?

A colleague questioned this change, and I am wondering if there is any resource/reference that I can use to backup Black's decision to format the code like?

PEP8 – Python代码样式指南和黑色代码样式.

演示

相关,但未回答我的问题:

Related, but doesn't answer my question: What is the proper way to format a multi-line dict in Python?

PS:#fmt:off 阻止Black格式化行,但是我不想使用它,因为我的团队通常不使用Black.

PS: # fmt: off prevents Black from formatting line, but I don't want to use it, since my team doesn't use Black in general.

推荐答案

黑色代码样式是检查的正确位置,而您是对的,对于这种用例而言,它不是很清晰.我可以说,如果您不将值的字符串分成两个字符串,那么Black会将其放在您可能更喜欢的一行上.我不确定Black是否有办法知道何时可以将2个字符串连接为一个字符串-请参阅讨论

The Black code style was the right place to check and you are right, it's not super clear for this use-case. I can say if you don't split the string of the value into two strings then Black will put it on one line which you may prefer. I'm not sure Black has a good way to know when it can concatenate 2 strings to one when it makes sense - see discussion here.

例如

mydict = {
    "key0": "value0",
    "key1": "long-two-lines-string-value1-does-not-fit-in-one-line-has-to-continue value1",
}

也许使用括号也会使值更易读?(这通常是我的首选)例如

Using parentheses will make value more readable too perhaps? (this is my go-to usually) e.g.

mydict = {
    "key0": "value0",
    "key1": (
        "long-two-lines-string-value1-does-not-fit-in-one-"
        "line-has-to-continue value1"
    ),
}

顺便说一句,注意到黑色并没有用双引号代替您的单引号.这是您用于项目的设置吗?

By the way, noticed black didn't replace your single quotes with double quotes; is that a setting you are using for your projects?

这篇关于具有多行值的PEP8多行字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆