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

前端 未结 5 716
刺人心
刺人心 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:28

    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.

提交回复
热议问题