Is there a difference between using a dict literal and a dict constructor?

前端 未结 10 2096
深忆病人
深忆病人 2020-11-28 02:26

Using PyCharm, I noticed it offers to convert a dict literal:

d = {
    \'one\': \'1\',
    \'two\': \'2\',
}

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 02:54

    I find the dict literal d = {'one': '1'} to be much more readable, your defining data, rather than assigning things values and sending them to the dict() constructor.

    On the other hand i have seen people mistype the dict literal as d = {'one', '1'} which in modern python 2.7+ will create a set.

    Despite this i still prefer to all-ways use the set literal because i think its more readable, personal preference i suppose.

提交回复
热议问题