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

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

    Also consider the fact that tokens that match for operators can't be used in the constructor syntax, i.e. dasherized keys.

    >>> dict(foo-bar=1)
    File "", line 1
    SyntaxError: keyword can't be an expression
    
    >>> {'foo-bar': 1}
    {'foo-bar': 1}
    

提交回复
热议问题