Difference between dict.clear() and assigning {} in Python

后端 未结 8 929
我在风中等你
我在风中等你 2020-11-28 19:57

In python, is there a difference between calling clear() and assigning {} to a dictionary? If yes, what is it? Example:

d = {\"stuff\":\"         


        
8条回答
  •  孤街浪徒
    2020-11-28 20:24

    d = {} will create a new instance for d but all other references will still point to the old contents. d.clear() will reset the contents, but all references to the same instance will still be correct.

提交回复
热议问题