赋值运算符后python如何赋值 [英] How does python assign values after assignment operator

查看:60
本文介绍了赋值运算符后python如何赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我敢肯定这是一个非常愚蠢的问题.但是python是如何给变量赋值的呢?

Okay a very silly question I'm sure. But how does python assign value to variables?

假设有一个变量 a 并被赋值为 a=2.因此,python 为变量分配了一个内存位置,a 现在指向包含值 2 的内存位置.现在,如果我分配一个变量 b=a 变量 b 也指向与变量 a 相同的位置.

Say there is a variable a and is assigned the value a=2. So python assigns a memory location to the variable and a now points to the memory location that contains the value 2. Now, if I assign a variable b=a the variable b also points to the same location as variable a.

现在.如果我分配一个变量 c=2 它仍然指向与 a 相同的内存位置,而不是指向一个新的内存位置.那么,python 是如何工作的呢?它是否首先检查所有先前分配的变量以检查它们中是否有任何共享相同的值,然后为其分配内存位置?

Now. If I assign a variable c=2 it still points to the same memory location as a instead of pointing to a new memory location. So, how does python work? Does it check first check all the previously assigned variables to check if any of them share the same values and then assign it the memory location?

此外,它与列表的工作方式不同.如果我分配 a=[2,3] 然后 b=[2,3] 并使用 id 函数检查它们的内存位置,我得到两个不同的内存位置.但是 c=b 给了我相同的位置.有人可以解释一下正确的工作方式和原因吗?

Also, it doesn't work the same way with lists. If I assign a=[2,3] and then b=[2,3] and check their memory locations with the id function, I get two different memory locations.But c=b gives me the same location. Can someone explain the proper working and reason for this?

-

基本上我的问题是因为我刚刚开始学习 is 运算符,显然只有当它们指向同一位置时它才保持 True .所以,如果 a=1000b=1000 a is bFalse 但是,a="world" b="world" 确实如此.

Basically my question is because I've just started learning about the is operator and apparently it holds True only if they are pointing to the same location. So, if a=1000 and b=1000 a is b is False but, a="world" b="world" it holds true.

推荐答案

我以前遇到过这个问题,我知道它会让人感到困惑.这里有两个概念:

I've faced this problem before and understand that it gets confusing. There are two concepts here:

  1. 有些数据结构是可变的,有些则不是
  2. Python 处理指针......大部分时间

那么让我们考虑一个列表的情况(当您使用 int 时,您不小心偶然发现了实习和窥视孔优化 - 我稍后会谈到)

So let's consider the case of a list (you accidentally stumbled on interning and peephole optimizations when you used ints - I'll get to that later)

所以让我们创建两个相同的列表(记住列表是可变的)

So let's create two identical lists (remember lists are mutable)

In [42]: a = [1,2]

In [43]: b = [1,2]

In [44]: id(a) == id(b)
Out[44]: False

In [45]: a is b
Out[45]: False

看,尽管列表是相同的,ab 是不同的内存位置.现在,这是因为 python 计算 [1,2],将其分配给一个内存位置,然后调用该位置 a(或 b).python检查每个分配的内存位置以查看[1,2]是否已经存在,将b分配到与<相同的内存位置需要相当长的时间代码>a.
更不用说列表是可变的,即您可以执行以下操作:

See, despite the fact that the lists are identical, a and b are different memory locations. Now, this is because python computes [1,2], assigns it to a memory location, and then calls that location a (or b). It would take quite a long time for python to check every allocated memory location to see if [1,2] already exists, to assign b to the same memory location as a.
And that's not to mention that lists are mutable, i.e. you can do the following:

In [46]: a = [1,2]

In [47]: id(a)
Out[47]: 4421968008

In [48]: a.append(3)

In [49]: a
Out[49]: [1, 2, 3]

In [50]: id(a)
Out[50]: 4421968008

看到了吗?a 保存的值已更改,但内存位置未更改.现在,如果一堆其他变量名被分配到同一个内存位置怎么办?!它们也会被更改,这将是语言的缺陷.为了解决这个问题,python 必须将整个列表复制到一个新的内存位置,只是因为我想更改 a

See that? The value that a holds has changed, but the memory location has not. Now, what if a bunch of other variable names were assigned to the same memory location?! they would be changed as well, which would be a flaw with the language. In order to fix this, python would have to copy over the entire list into a new memory location, just because I wanted to change the value of a

即使是空列表也是如此:

This is true even of empty lists:

In [51]: a = []

In [52]: b = []

In [53]: a is b
Out[53]: False

In [54]: id(a) == id(b)
Out[54]: False

现在,让我们谈谈我所说的关于指针的那些事:

Now, let's talk about that stuff I said about pointers:

假设您希望两个变量实际上谈论同一个内存位置.然后,您可以将第二个变量分配给第一个:

Let's say you want two variables to actually talk about the same memory location. Then, you could assign your second variable to your first:

In [55]: a = [1,2,3,4]

In [56]: b = a

In [57]: id(a) == id(b)
Out[57]: True

In [58]: a is b
Out[58]: True

In [59]: a[0]
Out[59]: 1

In [60]: b[0]
Out[60]: 1

In [61]: a
Out[61]: [1, 2, 3, 4]

In [62]: b
Out[62]: [1, 2, 3, 4]

In [63]: a.append(5)

In [64]: a
Out[64]: [1, 2, 3, 4, 5]

In [65]: b
Out[65]: [1, 2, 3, 4, 5]

In [66]: a is b
Out[66]: True

In [67]: id(a) == id(b)
Out[67]: True

In [68]: b.append(6)

In [69]: a
Out[69]: [1, 2, 3, 4, 5, 6]

In [70]: b
Out[70]: [1, 2, 3, 4, 5, 6]

In [71]: a is b
Out[71]: True

In [72]: id(a) == id(b)
Out[72]: True

看看那里发生了什么!ab 都分配到相同的内存位置.因此,您对其中一个所做的任何更改都会反映在另一个上.

Look what happened there! a and b are both assigned to the same memory location. Therefore, any changes you make to one, will be reflected on the other.

最后,让我们简单地谈谈我之前提到的那个窥视孔的东西.Python 试图节省空间.因此,它在启动时将一些小东西加载到内存中(例如小整数).因此,当你将一个变量赋给一个小整数(比如 5)时,python 不必在将值赋给内存位置之前计算 5,并且为其分配一个变量名称(与您的列表不同).因为它已经知道 5 是什么,并且把它藏在某个内存位置,所以它所做的就是为该内存位置分配一个变量名.但是,对于更大的整数,情况不再如此:

Lastly, let's talk briefly about that peephole stuff I mentioned before. Python tries to save space. So, it loads a few small things into memory when it starts up (small integers, for example). As a result, when you assign a variable to a small integer (like 5), python doesn't have to compute 5 before assigning the value to a memory location, and assigning a variable name to it (unlike it did in the case of your lists). Since it already knows what 5 is, and has it stashed away in some memory location, all it does is assign that memory location a variable name. However, for much larger integers, this is no longer the case:

In [73]: a = 5

In [74]: b = 5

In [75]: id(a) == id(b)
Out[75]: True

In [76]: a is b
Out[76]: True

In [77]: a = 1000000000

In [78]: b = 1000000000

In [79]: id(a) == id(b)
Out[79]: False

In [80]: a is b
Out[80]: False

这篇关于赋值运算符后python如何赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆