Why does Pycharm's inspector complain about “d = {}”?

前端 未结 5 722
刺人心
刺人心 2020-12-23 12:53

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

This dictionary creation could be rewrit

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 13:31

    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

提交回复
热议问题