Python Are name bindings not object references?

I am trying to understand what exactly is a Python name binding, and when is this binding interpreted.

In c

include <stdio.h>
int main()
{
int X = 42;
int* Y[1];
Y[0] = &X;
X = 666;
printf("%d", *Y[0]);
return 0;
}

prints 666. I was expecting a block of Python code:

X = 42
L = []
L.append(X) #3
X = 666
print(L) #5

do the same, but it’s not. What exactly happens between the lines labeled 3 and 5? # 3 makes another reference to an object known as "42", for example X, allows you to call it X 'and store X' in the object pointed to by L, which is []?

+2
source share
1 answer

What you say is almost what happens:

X = 42               # Create new object 42, bind name X to it.
L = []
L.append(X)          # Bind L[0] to the 42 object.
X = 666              # Create new object 666, bind name X to it.
print(L)             # Will not see the 666.

appenddoes not bind the array element to X, it binds it to the object behind X, which is 42.

, Python, ( , , ) .

+5

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