How python assigns values ​​after assignment operator

Well, a very stupid question, I'm sure. But how does python assign a value to variables?

Let's say that a variable exists aand is assigned a value a=2. So python assigns a memory cell to a variable, and anow points to a memory cell that contains the value 2. Now, if I assign a variable b=a, the variable balso points to the same place as the variable a.

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

In addition, it does not work with lists. If I assign a=[2,3]and then b=[2,3]check their places in memory using a function id, I get two different locations in memory. But c=bgives me the same location. Can someone explain the correct operation and the reason for this?

edit: -

Basically, my question is that I just started to study the operator isand, apparently, it Trueonly holds if they point to the same place. So, if a=1000and b=1000 a is bthere False, but it a="world" b="world"is performed.

+4
source share
3 answers

I ran into this problem and realized it was confusing. There are two concepts here:

  • some data structures are mutable while others are not
  • Python works with pointers ... most of the time

So, consider the case of the list (you accidentally stumbled upon optimizing internment and the eye when you used ints - I'll take care of this later)

, (, )

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

., , , a b - . , python [1,2], , a ( b). Python , , , [1,2], b , a.
, , .. :

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

:

In [51]: a = []

In [52]: b = []

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

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

, :

, , . :

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

, ! a b . , , .

, , . Python . , , (, ). , (, 5), python 5 ( ). , 5, - , , , . :

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
+5

, python . , a c, . , , - . , 1000 , ; , , -128 127 ( " " ).

0

, , , , python C ++. :

(OBJ) "" . ( ), . id().

, python . , , , , .

0

Source: https://habr.com/ru/post/1622156/


All Articles