|
|
发表于 2019-10-15 22:27:49
|
显示全部楼层
你这是重新赋值,id是会变的:
- Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
- Type "help", "copyright", "credits" or "license()" for more information.
- >>> old = [1, 2, 3, 4, 5]
- >>> new = old
- >>> id(old)
- 54668184
- >>> id(new)
- 54668184
- >>> old = [6]
- >>> id(old)
- 61375752
- >>> id(new)
- 54668184
- >>>
复制代码
如果你不想让id变,就不要重新赋值:
- Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
- Type "help", "copyright", "credits" or "license()" for more information.
- >>> old = [1, 2, 3, 4, 5]
- >>> new = old
- >>> old.clear()
- >>> old.append(6)
- >>> new
- [6]
- >>> old
- [6]
- >>> id(old)
- 56437696
- >>> id(new)
- 56437696
- >>>
复制代码 |
|