问题:Pycharm的检查员为何抱怨“ d = {}”?

d = {}Pycharm的代码检查器初始化字典时,会产生警告,说

这个字典的创建可以重写为字典文字。

如果我重写它d = dict(),警告就会消失。由于{}已经字典文字,因此我很确定该消息是错误的。此外,似乎都d = {}d = dict()有效,Python化。

这个相关的问题似乎可以得出结论,选择只取决于样式/偏好: “ d = dict()”和“ d = {}”之间的差异

皮查姆为什么会抱怨d = {}

更新:

Mac钉了它。该警告实际上适用于多行,而不仅仅是标记的行。

Pycharm似乎在寻找一系列连续语句,在这些语句中您初始化字典,然后在字典中设置值。例如,这将触发警告:

d = {}
d['a'] = 1

但是此代码不会:

d = {}
pass
d['a'] = 1

When initializing a dictionary with d = {} Pycharm’s code inspector generates a warning, saying

This dictionary creation could be rewritten as a dictionary literal.

If I rewrite it d = dict() the warning goes away. Since {} already is a dictionary literal, I’m pretty sure the message is erroneous. Furthermore, it seems like both d = {} and d = dict() are valid and Pythonic.

This related question seems to conclude that the choice is just a matter of style/preference: differences between “d = dict()” and “d = {}”

Why would Pycharm complain about d = {}?

UPDATE:

Mac nailed it. The warning actually applied to multiple lines, not just the one that was flagged.

Pycharm seems to look for a sequence of consecutive statements where you initialize a dictionary and then set values in the dictionary. For example, this will trigger the warning:

d = {}
d['a'] = 1

But this code will not:

d = {}
pass
d['a'] = 1

回答 0

字典声明中的以下代码是什么?

我认为pycharm将在出现以下情况时触发错误:

dic = {}
dic['aaa'] = 5

如你所写

dic = {'aaa': 5}

顺便说一句:如果您使用该函数,错误就会消失,但这并不一定意味着pycharm认为这dict()是文字。这可能只是意味着它不会抱怨:

dic = dict()
dic['aaa'] = 5

HTH!

What is the following code to your dictionary declaration?

I think pycharm will trigger the error if you have something like:

dic = {}
dic['aaa'] = 5

as you could have written

dic = {'aaa': 5}

BTW: The fact that the error goes away if you use the function doesn’t necessarily mean that pycharm believes dict() is a literal. It could just mean that it doesn’t complain for:

dic = dict()
dic['aaa'] = 5

HTH!


回答 1

可以在项目设置或默认设置中禁用它。

  • 导航到设置->检查-> Python
  • 取消选中“字典创建可以用字典文字重写”

This can be disabled in the Project Settings or Default Settings.

  • Navigate to Settings -> Inspections -> Python
  • Uncheck “Dictionary creation could be rewritten by dictionary literal”

回答 2

对于那些喜欢(就像我一样)通过单个操作初始化字典的人

d = {
  'a': 12,
  'b': 'foo',
  'c': 'bar'
}

而不是像

d = dict()
d['a'] = 12
d['b'] = ....

最后,我得出以下结论:

d = dict()
d.update({
  'a': 12,
  'b': 'foo',
  'c': 'bar'
})

Pycharm没有对此抱怨

for those who like (just like me) to initialize dictionaries with single operation

d = {
  'a': 12,
  'b': 'foo',
  'c': 'bar'
}

instead of many lines like

d = dict()
d['a'] = 12
d['b'] = ....

in the end I ended up with this:

d = dict()
d.update({
  'a': 12,
  'b': 'foo',
  'c': 'bar'
})

Pycharm is not complaining on this


回答 3

mydict = {
  a: 5,
  b:z+c/2
}

可以直接创建字典而无需先初始化字典,然后重新分配新值。

mydict = {
  a: 5,
  b:z+c/2
}

The dictionary could have been created directly without initialising them first and then reassigning new values.


回答 4

我遇到这样的情况,该警告使我烦恼不已。就我而言,我将字典部分填充为文字,并部分地从函数的元组输出中填充,如下所示:

def get_other_values():
    return 3, 4

foo = {
    "a": 1,
    "b": 2
}
foo["c"], foo["d"] = get_other_values()

因此,除非我为get_other_values的输出创建临时var,否则即使我使用文字创建dict,PEP8也会生成此警告。而且我无法在文字中分配c和d键,因为这些值以元组形式输出。

I have a situation where this warning is bugging the hell out of me. In my case, I’m populating my dict partially as literals and partially from a tuple output by a function, like so:

def get_other_values():
    return 3, 4

foo = {
    "a": 1,
    "b": 2
}
foo["c"], foo["d"] = get_other_values()

So, unless I create interim vars for the output of get_other_values, PEP8 generates this warning even though I’m creating the dict with literals. And I can’t assign the c and d keys in the literal, since the values are output as a tuple.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。