About the changing id of an immutable string

后端 未结 5 1832
暗喜
暗喜 2020-11-22 03:33

Something about the id of objects of type str (in python 2.7) puzzles me. The str type is immutable, so I would expect that once it is

5条回答
  •  借酒劲吻你
    2020-11-22 04:24

    In your first example a new instance of the string 'so' is created each time, hence different id.

    In the second example you are binding the string to a variable and Python can then maintain a shared copy of the string.

提交回复
热议问题