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

前端 未结 10 2085
深忆病人
深忆病人 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:32

    I think you have pointed out the most obvious difference. Apart from that,

    the first doesn't need to lookup dict which should make it a tiny bit faster

    the second looks up dict in locals() and then globals() and the finds the builtin, so you can switch the behaviour by defining a local called dict for example although I can't think of anywhere this would be a good idea apart from maybe when debugging

提交回复
热议问题