[Python] Understand Mutable vs. Immutable objects in Python

In this lesson, you will learn what mutable and immutable objects are, and the difference between them. This understanding will help you determine when objects can be modified in place, and when new objects must be created.

List is mutable, which means everytime it returns the same id whether or not you have changed it:

foo = []
id(foo) // same
foo.append(3)
id(foo) // same

Immtuable such as string:

str = "Hello"
id(str) // not the same
str = "World"
id(str) // not the same
原文地址:https://www.cnblogs.com/Answer1215/p/8012188.html