官术网_书友最值得收藏!

Mutable or immutable? That is the question

A first fundamental distinction that Python makes on data is about whether or not the value of an object changes. If the value can change, the object is called mutable, while if the value cannot change, the object is called immutable.

It is very important that you understand the distinction between mutable and immutable because it affects the code you write, so here's a question:

>>> age = 42
>>> age
42
>>> age = 43 #A
>>> age
43

In the preceding code, on the line #A, have I changed the value of age? Well, no. But now it's 43 (I hear you say...). Yes, it's 43, but 42 was an integer number, of the type int, which is immutable. So, what happened is really that on the first line, age is a name that is set to point to an int object, whose value is 42. When we type age = 43, what happens is that another object is created, of the type int and value 43 (also, the id will be different), and the name age is set to point to it. So, we didn't change that 42 to 43. We actually just pointed age to a different location: the new int object whose value is 43. Let's see the same code also printing the IDs:

>>> age = 42
>>> id(age)
10456352
>>> age = 43
>>> id(age)
10456384

Notice that we print the IDs by calling the built-in id function. As you can see, they are different, as expected. Bear in mind that age points to one object at a time: 42 first, then 43. Never together.

Now, let's see the same example using a mutable object. For this example, let's just use a Person object, that has a property age:

>>> fab = Person(age=39)
>>> fab.age
39
>>> id(fab)
139632387887456
>>> fab.age = 29 # I wish!
>>> id(fab)
139632387887456 # still the same id

In this case, I set up an object fab whose type is Person (a custom class). On creation, the object is given the age of 39. I'm printing it, along with the object id, right afterwards. Notice that, even after I change age to be 29, the ID of fab stays the same (while the ID of age has changed, of course). Custom objects in Python are mutable (unless you code them not to be). Keep this concept in mind, it's very important. I'll remind you about it through the rest of the chapter.

主站蜘蛛池模板: 霍州市| 民县| 印江| 蒙阴县| 木兰县| 密山市| 临夏县| 平顶山市| 九龙坡区| 永定县| 泉州市| 肃南| 沙坪坝区| 阿克陶县| 哈巴河县| 昌乐县| 奎屯市| 邵武市| 于都县| 武穴市| 师宗县| 稷山县| 长宁县| 神池县| 漯河市| 永吉县| 凤山市| 公安县| 柏乡县| 咸丰县| 龙胜| 屯留县| 濮阳市| 聂拉木县| 甘孜县| 达日县| 龙口市| 象州县| 南部县| 汤阴县| 富民县|