能否用C++的指针/引用来类比解释一下Python中Tuple的“指向不变性”?
怪我咯
怪我咯 2017-04-17 16:32:10
0
6
492

初学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赋值,却有两种不同的意义”的矛盾。有什么解释方式能调和这种矛盾么?】

表述的不太清楚,希望能有理解了我的疑问的朋友呀。多谢了。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(6)
洪涛

Your analogy is wrong. myTuple=(myInt,myList) is at any time: myTuple=(myInt,myList)在任何时候都是:

&myTuple[0]=myInt;
&myTuple[1]=myList;

因为在Python里一切都是对象,myInt也是。tupleimmutable的意思是你无法为其元素重新赋值。也就是你不能改变myTuple[0]myTuple[1]这两个指针变量的值,所以它们永远指向myIntmyList,这一点是确定的。

但是你可以修改tuple中的每个元素所指向的对象本身,前提是这个对象必须是可变的。比如你能够修改myList,因为这是一个可变对象。但是myInt rrreee

Because everything in Python is an object, and so is myInt. tuple is immutable which means that you cannot reassign its elements. That is to say, you cannot change the values ​​of the two pointer variables myTuple[0] and myTuple[1], so they always point to myInt and myList, that's for sure. 🎜 🎜But you can modify the object itself pointed to by each element in tuple, provided that the object must be mutable. For example, you can modify myList because it is a mutable object. But myInt 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:

  1. 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).

  2. 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.

  3. 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.

Peter_Zhu
myInt = 5
myList = [3]
print id(myList)  #id: 37216584
myTuple = (myInt, myList)
print myTuple  #(5, [3])
myList.append(4)
print id(myList) #id: 37216584
print myTuple #(5, [3, 4])
myList = [4, 5]
print id(myList)# id: 37258584
print myTuple #(5, [3, 4])

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template