我在python中使用id()
函数。我得到了一些有趣的观察结果。
请参阅下面给出的代码:
#Python Code
import numpy as np
x = np.arange(1,10)
x = np.reshape(x, (3,3))
# x is array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]])
y = x[:2,1:]
y[1,0] = 50
# Now y is array([[ 2, 3], [50, 6]])
# and x is array([[ 1, 2, 3], [ 4, 50, 6], [ 7, 8, 9]])
yc = np.copy(x[:2,1:])
yc[0,1] = 300
# Now y is array([[ 2, 3], [50, 6]])
# and x is array([[ 1, 2, 3], [ 4, 50, 6], [ 7, 8, 9]])
# and yc is array([[ 2, 300], [50, 6]]) '''
print(id(x[0,2]), id(y[0,1]), id(yc[0,1]))
#170516080 170516080 170516080
#All are same !!
id(x[0,2]) == id(yc[0,1])
#True
x[0,2] is yc[0,1]
#False
is
与id(obj1) == id(obj2)
的不同之处是什么?
谁能解释一下呢?