复制和修改过的对象如何在python中具有相同的id?

时间:2017-04-21 04:17:13

标签: python memory-management

我在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

isid(obj1) == id(obj2)的不同之处是什么? 谁能解释一下呢?

0 个答案:

没有答案