1

I was playing with id() function in python. I got some interesting observations.

See the piece of code given below:

    #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

So how is is different form id(obj1) == id(obj2) ? Can anyone explain this?

Arg0
  • 11
  • 1
  • 4
    Possible duplicate of [\`id\` function in Python 2.7, \`is\` operator, object identity and user-defined methods](http://stackoverflow.com/questions/25851090/id-function-in-python-2-7-is-operator-object-identity-and-user-defined-met) – Arya McCarthy Apr 21 '17 at 04:25
  • The `is` keyword is checking for location in memory. Your pointers move things around in memory but do not change the `id`. – JacobIRR Apr 21 '17 at 05:01

0 Answers0