“is” operation returns false even though two objects have same id The 2019 Stack Overflow...

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

How to make Illustrator type tool selection automatically adapt with text length

What do I do when my TA workload is more than expected?

Do I have Disadvantage attacking with an off-hand weapon?

Solving overdetermined system by QR decomposition

One-dimensional Japanese puzzle

Can withdrawing asylum be illegal?

Can I visit the Trinity College (Cambridge) library and see some of their rare books

Word for: a synonym with a positive connotation?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Can each chord in a progression create its own key?

Do working physicists consider Newtonian mechanics to be "falsified"?

Working through Single Responsibility Principle in Python when Calls are Expensive

Why doesn't shell automatically fix "useless use of cat"?

Categorical vs continuous feature selection/engineering

Can we generate random numbers using irrational numbers like π and e?

How to read αἱμύλιος or when to aspirate

Didn't get enough time to take a Coding Test - what to do now?

"... to apply for a visa" or "... and applied for a visa"?

Did the new image of black hole confirm the general theory of relativity?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Does Parliament need to approve the new Brexit delay to 31 October 2019?

What aspect of planet earth must be changed to prevent the industrial revolution?



“is” operation returns false even though two objects have same id



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to return multiple values from a function?Does Python have a ternary conditional operator?Relationship between SciPy and NumPyCreating a singleton in PythonPython: two objects are the sameWhy is [] faster than list()?Why does “not(True) in [False, True]” return False?is comparison returns False with strings using same idHow can two Python objects have same id but 'is' operator returns False?Comparing Objects - Why is == returning 'False' in the following example?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







8















Two python objects have the same id but "is" operation returns false as shown below:



a = np.arange(12).reshape(2, -1)
c = a.reshape(12, 1)
print("id(c.data)", id(c.data))
print("id(a.data)", id(a.data))

print(c.data is a.data)
print(id(c.data) == id(a.data))


Here is the actual output:



id(c.data) 241233112
id(a.data) 241233112
False
True


My question is... why "c.data is a.data" returns false even though they point to the same ID, thus pointing to the same object? I thought that they point to the same object if they have same ID or am I wrong? Thank you!










share|improve this question




















  • 1





    @C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

    – chepner
    1 hour ago






  • 2





    @C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

    – chepner
    1 hour ago






  • 1





    @C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

    – juanpa.arrivillaga
    1 hour ago






  • 1





    References aren't shared between two variables; each variable is a reference to the object it refers to.

    – chepner
    1 hour ago






  • 1





    @C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

    – juanpa.arrivillaga
    1 hour ago


















8















Two python objects have the same id but "is" operation returns false as shown below:



a = np.arange(12).reshape(2, -1)
c = a.reshape(12, 1)
print("id(c.data)", id(c.data))
print("id(a.data)", id(a.data))

print(c.data is a.data)
print(id(c.data) == id(a.data))


Here is the actual output:



id(c.data) 241233112
id(a.data) 241233112
False
True


My question is... why "c.data is a.data" returns false even though they point to the same ID, thus pointing to the same object? I thought that they point to the same object if they have same ID or am I wrong? Thank you!










share|improve this question




















  • 1





    @C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

    – chepner
    1 hour ago






  • 2





    @C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

    – chepner
    1 hour ago






  • 1





    @C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

    – juanpa.arrivillaga
    1 hour ago






  • 1





    References aren't shared between two variables; each variable is a reference to the object it refers to.

    – chepner
    1 hour ago






  • 1





    @C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

    – juanpa.arrivillaga
    1 hour ago














8












8








8








Two python objects have the same id but "is" operation returns false as shown below:



a = np.arange(12).reshape(2, -1)
c = a.reshape(12, 1)
print("id(c.data)", id(c.data))
print("id(a.data)", id(a.data))

print(c.data is a.data)
print(id(c.data) == id(a.data))


Here is the actual output:



id(c.data) 241233112
id(a.data) 241233112
False
True


My question is... why "c.data is a.data" returns false even though they point to the same ID, thus pointing to the same object? I thought that they point to the same object if they have same ID or am I wrong? Thank you!










share|improve this question
















Two python objects have the same id but "is" operation returns false as shown below:



a = np.arange(12).reshape(2, -1)
c = a.reshape(12, 1)
print("id(c.data)", id(c.data))
print("id(a.data)", id(a.data))

print(c.data is a.data)
print(id(c.data) == id(a.data))


Here is the actual output:



id(c.data) 241233112
id(a.data) 241233112
False
True


My question is... why "c.data is a.data" returns false even though they point to the same ID, thus pointing to the same object? I thought that they point to the same object if they have same ID or am I wrong? Thank you!







python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









ribitskiyb

376




376










asked 2 hours ago









drminixdrminix

513




513








  • 1





    @C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

    – chepner
    1 hour ago






  • 2





    @C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

    – chepner
    1 hour ago






  • 1





    @C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

    – juanpa.arrivillaga
    1 hour ago






  • 1





    References aren't shared between two variables; each variable is a reference to the object it refers to.

    – chepner
    1 hour ago






  • 1





    @C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

    – juanpa.arrivillaga
    1 hour ago














  • 1





    @C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

    – chepner
    1 hour ago






  • 2





    @C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

    – chepner
    1 hour ago






  • 1





    @C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

    – juanpa.arrivillaga
    1 hour ago






  • 1





    References aren't shared between two variables; each variable is a reference to the object it refers to.

    – chepner
    1 hour ago






  • 1





    @C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

    – juanpa.arrivillaga
    1 hour ago








1




1





@C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

– chepner
1 hour ago





@C.Nivs They don't even necessarily have different memory addresses (something which Python doesn't expose). Whatever memory was used for the first may have been reused for the second.

– chepner
1 hour ago




2




2





@C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

– chepner
1 hour ago





@C.Nivs Don't think of it in terms of memory addresses. How memory is managed is completely implementation dependent. All you know for sure is that two objects that overlap in time will not have the same id.

– chepner
1 hour ago




1




1





@C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

– juanpa.arrivillaga
1 hour ago





@C.Nivs no, ids do not belong to variables. They belong to objects. Many variables can reference the same object.

– juanpa.arrivillaga
1 hour ago




1




1





References aren't shared between two variables; each variable is a reference to the object it refers to.

– chepner
1 hour ago





References aren't shared between two variables; each variable is a reference to the object it refers to.

– chepner
1 hour ago




1




1





@C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

– juanpa.arrivillaga
1 hour ago





@C.Nivs yes, you need to understand, Python variables are not like C variables. The best way is to think of a Python variable as literally a key in a dict. This is actually true for the global scope, but modern python optimizes local scopes as essentially arrays/symbol tables. In any case, just as you can have 100 references to the same object in a dict, a variable can reference the same object, or a different object, and the ID belongs to the object, not the variable.

– juanpa.arrivillaga
1 hour ago












2 Answers
2






active

oldest

votes


















11














a.data and c.data both produce a transient object, with no reference to it. As such, both are immediately garbage-collected. The same id can be used for both.



In your first if statement, the objects have to co-exist while is checks if they are identical, which they are not.



In the second if statement, each object is released as soon as id returns its id.



If you save references to both objects, keeping them alive, you can see they are not the same object.



r0 = a.data
r1 = c.data
assert r0 is not r1





share|improve this answer





















  • 2





    what is confusing is the fact that data looks like an attribute, but is probably a property

    – Jean-François Fabre
    1 hour ago











  • In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

    – amanb
    1 hour ago











  • @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

    – C.Nivs
    1 hour ago






  • 3





    a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

    – Jean-François Fabre
    1 hour ago



















3














In [62]: a = np.arange(12).reshape(2,-1) 
...: c = a.reshape(12,1)


.data returns a memoryview object. id just gives the id of that object; it's not the value of the object, or any indication of where a databuffer is located.



In [63]: a.data                                                                 
Out[63]: <memory at 0x7f672d1101f8>
In [64]: c.data
Out[64]: <memory at 0x7f672d1103a8>
In [65]: type(a.data)
Out[65]: memoryview


https://docs.python.org/3/library/stdtypes.html#memoryview



If you want to verify that a and c share a data buffer, I find the __array_interface__ to be a better tool.



In [66]: a.__array_interface__['data']                                          
Out[66]: (50988640, False)
In [67]: c.__array_interface__['data']
Out[67]: (50988640, False)


It even shows the offset produced by slicing - here 24 bytes, 3*8



In [68]: c[3:].__array_interface__['data']                                      
Out[68]: (50988664, False)




I haven't seen much use of a.data. It can be used as the buffer object when creating a new array with ndarray:



In [70]: d = np.ndarray((2,6), dtype=a.dtype, buffer=a.data)                    
In [71]: d
Out[71]:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
In [72]: d.__array_interface__['data']
Out[72]: (50988640, False)


But normally we create new arrays with shared memory with slicing or np.array (copy=False).






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55658189%2fis-operation-returns-false-even-though-two-objects-have-same-id%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    a.data and c.data both produce a transient object, with no reference to it. As such, both are immediately garbage-collected. The same id can be used for both.



    In your first if statement, the objects have to co-exist while is checks if they are identical, which they are not.



    In the second if statement, each object is released as soon as id returns its id.



    If you save references to both objects, keeping them alive, you can see they are not the same object.



    r0 = a.data
    r1 = c.data
    assert r0 is not r1





    share|improve this answer





















    • 2





      what is confusing is the fact that data looks like an attribute, but is probably a property

      – Jean-François Fabre
      1 hour ago











    • In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

      – amanb
      1 hour ago











    • @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

      – C.Nivs
      1 hour ago






    • 3





      a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

      – Jean-François Fabre
      1 hour ago
















    11














    a.data and c.data both produce a transient object, with no reference to it. As such, both are immediately garbage-collected. The same id can be used for both.



    In your first if statement, the objects have to co-exist while is checks if they are identical, which they are not.



    In the second if statement, each object is released as soon as id returns its id.



    If you save references to both objects, keeping them alive, you can see they are not the same object.



    r0 = a.data
    r1 = c.data
    assert r0 is not r1





    share|improve this answer





















    • 2





      what is confusing is the fact that data looks like an attribute, but is probably a property

      – Jean-François Fabre
      1 hour ago











    • In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

      – amanb
      1 hour ago











    • @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

      – C.Nivs
      1 hour ago






    • 3





      a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

      – Jean-François Fabre
      1 hour ago














    11












    11








    11







    a.data and c.data both produce a transient object, with no reference to it. As such, both are immediately garbage-collected. The same id can be used for both.



    In your first if statement, the objects have to co-exist while is checks if they are identical, which they are not.



    In the second if statement, each object is released as soon as id returns its id.



    If you save references to both objects, keeping them alive, you can see they are not the same object.



    r0 = a.data
    r1 = c.data
    assert r0 is not r1





    share|improve this answer















    a.data and c.data both produce a transient object, with no reference to it. As such, both are immediately garbage-collected. The same id can be used for both.



    In your first if statement, the objects have to co-exist while is checks if they are identical, which they are not.



    In the second if statement, each object is released as soon as id returns its id.



    If you save references to both objects, keeping them alive, you can see they are not the same object.



    r0 = a.data
    r1 = c.data
    assert r0 is not r1






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    chepnerchepner

    262k35251345




    262k35251345








    • 2





      what is confusing is the fact that data looks like an attribute, but is probably a property

      – Jean-François Fabre
      1 hour ago











    • In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

      – amanb
      1 hour ago











    • @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

      – C.Nivs
      1 hour ago






    • 3





      a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

      – Jean-François Fabre
      1 hour ago














    • 2





      what is confusing is the fact that data looks like an attribute, but is probably a property

      – Jean-François Fabre
      1 hour ago











    • In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

      – amanb
      1 hour ago











    • @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

      – C.Nivs
      1 hour ago






    • 3





      a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

      – Jean-François Fabre
      1 hour ago








    2




    2





    what is confusing is the fact that data looks like an attribute, but is probably a property

    – Jean-François Fabre
    1 hour ago





    what is confusing is the fact that data looks like an attribute, but is probably a property

    – Jean-François Fabre
    1 hour ago













    In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

    – amanb
    1 hour ago





    In my tests, the id's are different in the first run, but change to become the same on subsequent runs.

    – amanb
    1 hour ago













    @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

    – C.Nivs
    1 hour ago





    @Jean-FrançoisFabre so would that mean that the object itself is only returned when a getter is called, and the property is not actually stored in the class? I'm not quite familiar with the differences between a property vs attribute

    – C.Nivs
    1 hour ago




    3




    3





    a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

    – Jean-François Fabre
    1 hour ago





    a property is a method disguised as an attribute. So it can return a discardable integer, object, whatever.

    – Jean-François Fabre
    1 hour ago













    3














    In [62]: a = np.arange(12).reshape(2,-1) 
    ...: c = a.reshape(12,1)


    .data returns a memoryview object. id just gives the id of that object; it's not the value of the object, or any indication of where a databuffer is located.



    In [63]: a.data                                                                 
    Out[63]: <memory at 0x7f672d1101f8>
    In [64]: c.data
    Out[64]: <memory at 0x7f672d1103a8>
    In [65]: type(a.data)
    Out[65]: memoryview


    https://docs.python.org/3/library/stdtypes.html#memoryview



    If you want to verify that a and c share a data buffer, I find the __array_interface__ to be a better tool.



    In [66]: a.__array_interface__['data']                                          
    Out[66]: (50988640, False)
    In [67]: c.__array_interface__['data']
    Out[67]: (50988640, False)


    It even shows the offset produced by slicing - here 24 bytes, 3*8



    In [68]: c[3:].__array_interface__['data']                                      
    Out[68]: (50988664, False)




    I haven't seen much use of a.data. It can be used as the buffer object when creating a new array with ndarray:



    In [70]: d = np.ndarray((2,6), dtype=a.dtype, buffer=a.data)                    
    In [71]: d
    Out[71]:
    array([[ 0, 1, 2, 3, 4, 5],
    [ 6, 7, 8, 9, 10, 11]])
    In [72]: d.__array_interface__['data']
    Out[72]: (50988640, False)


    But normally we create new arrays with shared memory with slicing or np.array (copy=False).






    share|improve this answer






























      3














      In [62]: a = np.arange(12).reshape(2,-1) 
      ...: c = a.reshape(12,1)


      .data returns a memoryview object. id just gives the id of that object; it's not the value of the object, or any indication of where a databuffer is located.



      In [63]: a.data                                                                 
      Out[63]: <memory at 0x7f672d1101f8>
      In [64]: c.data
      Out[64]: <memory at 0x7f672d1103a8>
      In [65]: type(a.data)
      Out[65]: memoryview


      https://docs.python.org/3/library/stdtypes.html#memoryview



      If you want to verify that a and c share a data buffer, I find the __array_interface__ to be a better tool.



      In [66]: a.__array_interface__['data']                                          
      Out[66]: (50988640, False)
      In [67]: c.__array_interface__['data']
      Out[67]: (50988640, False)


      It even shows the offset produced by slicing - here 24 bytes, 3*8



      In [68]: c[3:].__array_interface__['data']                                      
      Out[68]: (50988664, False)




      I haven't seen much use of a.data. It can be used as the buffer object when creating a new array with ndarray:



      In [70]: d = np.ndarray((2,6), dtype=a.dtype, buffer=a.data)                    
      In [71]: d
      Out[71]:
      array([[ 0, 1, 2, 3, 4, 5],
      [ 6, 7, 8, 9, 10, 11]])
      In [72]: d.__array_interface__['data']
      Out[72]: (50988640, False)


      But normally we create new arrays with shared memory with slicing or np.array (copy=False).






      share|improve this answer




























        3












        3








        3







        In [62]: a = np.arange(12).reshape(2,-1) 
        ...: c = a.reshape(12,1)


        .data returns a memoryview object. id just gives the id of that object; it's not the value of the object, or any indication of where a databuffer is located.



        In [63]: a.data                                                                 
        Out[63]: <memory at 0x7f672d1101f8>
        In [64]: c.data
        Out[64]: <memory at 0x7f672d1103a8>
        In [65]: type(a.data)
        Out[65]: memoryview


        https://docs.python.org/3/library/stdtypes.html#memoryview



        If you want to verify that a and c share a data buffer, I find the __array_interface__ to be a better tool.



        In [66]: a.__array_interface__['data']                                          
        Out[66]: (50988640, False)
        In [67]: c.__array_interface__['data']
        Out[67]: (50988640, False)


        It even shows the offset produced by slicing - here 24 bytes, 3*8



        In [68]: c[3:].__array_interface__['data']                                      
        Out[68]: (50988664, False)




        I haven't seen much use of a.data. It can be used as the buffer object when creating a new array with ndarray:



        In [70]: d = np.ndarray((2,6), dtype=a.dtype, buffer=a.data)                    
        In [71]: d
        Out[71]:
        array([[ 0, 1, 2, 3, 4, 5],
        [ 6, 7, 8, 9, 10, 11]])
        In [72]: d.__array_interface__['data']
        Out[72]: (50988640, False)


        But normally we create new arrays with shared memory with slicing or np.array (copy=False).






        share|improve this answer















        In [62]: a = np.arange(12).reshape(2,-1) 
        ...: c = a.reshape(12,1)


        .data returns a memoryview object. id just gives the id of that object; it's not the value of the object, or any indication of where a databuffer is located.



        In [63]: a.data                                                                 
        Out[63]: <memory at 0x7f672d1101f8>
        In [64]: c.data
        Out[64]: <memory at 0x7f672d1103a8>
        In [65]: type(a.data)
        Out[65]: memoryview


        https://docs.python.org/3/library/stdtypes.html#memoryview



        If you want to verify that a and c share a data buffer, I find the __array_interface__ to be a better tool.



        In [66]: a.__array_interface__['data']                                          
        Out[66]: (50988640, False)
        In [67]: c.__array_interface__['data']
        Out[67]: (50988640, False)


        It even shows the offset produced by slicing - here 24 bytes, 3*8



        In [68]: c[3:].__array_interface__['data']                                      
        Out[68]: (50988664, False)




        I haven't seen much use of a.data. It can be used as the buffer object when creating a new array with ndarray:



        In [70]: d = np.ndarray((2,6), dtype=a.dtype, buffer=a.data)                    
        In [71]: d
        Out[71]:
        array([[ 0, 1, 2, 3, 4, 5],
        [ 6, 7, 8, 9, 10, 11]])
        In [72]: d.__array_interface__['data']
        Out[72]: (50988640, False)


        But normally we create new arrays with shared memory with slicing or np.array (copy=False).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        hpauljhpaulj

        118k787160




        118k787160






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55658189%2fis-operation-returns-false-even-though-two-objects-have-same-id%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Энтрерриос (город) Содержание История | Географическое...