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

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

    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!

提交回复
热议问题