初学Python,有一定C/C++基础。
看到Tuple的"immutable"的时候产生了这样一个疑问。
根据我的理解,如果Tuple的元素是普通变量(e.g. myInt=5),那创建
myTuple=(myInt,)
的意义就是
myTuple=(myInt的值,)
因此是不变的。
但是如果myTuple=(myInt,myList),虽然myTuple[1]永远都指向myList,但是myList中的内容是可以改变的。看到有人表示这Tuple里的List实际上是myList的引用。那能否用C++的指针/引用来类比统一解释一下这种Tuple元素的immutable特性呢?
【补充一下:比如对于myTuple=(myInt,myList),定义的时候,从意义上感觉(非严谨),就像是用C++写:
myTuple[0]=myInt; //myInt之后发生了什么都和myTuple[0]无关了,myTuple[0]只决定于myInt当前值
&myTuple[1]=myList; //而myTuple[1]更像是myList的一个引用。myList如果变化,myTuple[1]也会变化
于是这里就产生了“同样是对Tuple赋值,却有两种不同的意义”的矛盾。有什么解释方式能调和这种矛盾么?】
表述的不太清楚,希望能有理解了我的疑问的朋友呀。多谢了。
Your analogy is wrong.
myTuple=(myInt,myList)
is at any time:myTuple=(myInt,myList)
在任何时候都是:因为在Python里一切都是对象,
myInt
也是。tuple
是immutable
的意思是你无法为其元素重新赋值。也就是你不能改变myTuple[0]
和myTuple[1]
这两个指针变量的值,所以它们永远指向myInt
和myList
,这一点是确定的。但是你可以修改
Because everything in Python is an object, and so istuple
中的每个元素所指向的对象本身,前提是这个对象必须是可变的。比如你能够修改myList
,因为这是一个可变对象。但是myInt
rrreeemyInt
.tuple
isimmutable
which means that you cannot reassign its elements. That is to say, you cannot change the values of the two pointer variablesmyTuple[0]
andmyTuple[1]
, so they always point tomyInt
and myList, that's for sure. 🎜 🎜But you can modify the object itself pointed to by each element intuple
, provided that the object must be mutable. For example, you can modifymyList
because it is a mutable object. ButmyInt
doesn't work, because integers are also immutable objects, so you can't modify it. 🎜You understand the pointer as a small piece of paper with a number written on it. The number on the paper tells you which numbered drawer you should go to get something from.
Then the Tuple is like this:
When you have a myTuple, you have a note named myTuple. According to the number, you can find a drawer with one or more other small notes in the drawer. And the number of these slips is certain, and it may not change until you throw away the slip myTuple (or replace the number on myTuple with another one).
myTuple The process from
(myInt,)
变成(myInt, myList)
is actually to rewrite the number on myTuple, and put two notes into another drawer corresponding to the new number: the first one is the original myInt note, and the second one is The newly added myList note.When you add or delete any element to myList, you actually go to the corresponding drawer to do things based on the myList note. It has nothing to do with myTuple. It's just that there happens to be a number on the note in myTuple, which is different from the myTuple note. The numbers are the same.
It’s just the constant pointer.
I think it can be explained by the scenario of function passing parameters,
1. Variable object passing by reference: It can be changed on the original object. The pointer to the object is held both inside and outside the function. An object is used inside and outside the function. If it is changed inside the function The object will also be changed outside the function, similar to passing by reference.
2. Transfer copy of immutable objects: Immutable objects cannot change in the original memory space, so if you want to change its value, the interpreter will create a new object to store the new variables. At this time, the object used inside and outside the function is not the same object. --The previous object is still used outside the function, but a brand new object is used inside the function.
First make sure that myList is a list object, pointing to an address (or reference, I think it is like a pointer), and myTuple stores this reference. Because myList is variable, it can be added and deleted, but its address has not changed. Look at the id twice. If you point myList to another object, the address changes. The changed myList at this time will not affect myTuple. Because the list object saved by Tuple has not changed, it has nothing to do with myTuple.
In python, your myInt here is also a reference, pointing to an integer entity. This is different from c/c++. In c/c++, myInt is the integer entity itself; in this way, everything stored in the Tuple is a reference. The difference is that Int is immutable and List is mutable;
So the contradiction you mentioned does not exist.