“is” operation returns false even though two objects have same id [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?How 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?

Random body shuffle every night—can we still function?

Why are current probes so expensive?

Shimano 105 brifters (5800) and Avid BB5 compatibility

calculator's angle answer for trig ratios that can work in more than 1 quadrant on the unit circle

IC on Digikey is 5x more expensive than board containing same IC on Alibaba: How?

Understanding piped commands in GNU/Linux

How to resize main filesystem

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Problem with display of presentation

French equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

Does a random sequence of vectors span a Hilbert space?

Getting representations of the Lie group out of representations of its Lie algebra

Does the universe have a fixed centre of mass?

How to name indistinguishable henchmen in a screenplay?

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

Was the pager message from Nick Fury to Captain Marvel unnecessary?

Meaning of 境 in その日を境に

How to make triangles with rounded sides and corners? (squircle with 3 sides)

Did any compiler fully use 80-bit floating point?

Is there a spell that can create a permanent fire?

How do you cope with tons of web fonts when copying and pasting from web pages?

Vertical ranges of Column Plots in 12

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?How 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;








10
















This question already has an answer here:



  • id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?

    1 answer



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















marked as duplicate by ivan_pozdeev, smci, Community Apr 13 at 6:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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
    Apr 12 at 19:29






  • 3





    @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
    Apr 12 at 19:32






  • 1





    @Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

    – amanb
    Apr 12 at 19:35






  • 4





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

    – juanpa.arrivillaga
    Apr 12 at 19:55






  • 2





    @juanpa.arrivillaga fair enough. Thanks for the explanation

    – C.Nivs
    Apr 12 at 21:19

















10
















This question already has an answer here:



  • id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?

    1 answer



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















marked as duplicate by ivan_pozdeev, smci, Community Apr 13 at 6:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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
    Apr 12 at 19:29






  • 3





    @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
    Apr 12 at 19:32






  • 1





    @Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

    – amanb
    Apr 12 at 19:35






  • 4





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

    – juanpa.arrivillaga
    Apr 12 at 19:55






  • 2





    @juanpa.arrivillaga fair enough. Thanks for the explanation

    – C.Nivs
    Apr 12 at 21:19













10












10








10









This question already has an answer here:



  • id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?

    1 answer



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

















This question already has an answer here:



  • id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?

    1 answer



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!





This question already has an answer here:



  • id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?

    1 answer







python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 at 3:43









user2357112

159k13177272




159k13177272










asked Apr 12 at 19:19









drminixdrminix

615




615




marked as duplicate by ivan_pozdeev, smci, Community Apr 13 at 6:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by ivan_pozdeev, smci, Community Apr 13 at 6:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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
    Apr 12 at 19:29






  • 3





    @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
    Apr 12 at 19:32






  • 1





    @Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

    – amanb
    Apr 12 at 19:35






  • 4





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

    – juanpa.arrivillaga
    Apr 12 at 19:55






  • 2





    @juanpa.arrivillaga fair enough. Thanks for the explanation

    – C.Nivs
    Apr 12 at 21:19












  • 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
    Apr 12 at 19:29






  • 3





    @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
    Apr 12 at 19:32






  • 1





    @Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

    – amanb
    Apr 12 at 19:35






  • 4





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

    – juanpa.arrivillaga
    Apr 12 at 19:55






  • 2





    @juanpa.arrivillaga fair enough. Thanks for the explanation

    – C.Nivs
    Apr 12 at 21:19







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
Apr 12 at 19:29





@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
Apr 12 at 19:29




3




3





@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
Apr 12 at 19:32





@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
Apr 12 at 19:32




1




1





@Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

– amanb
Apr 12 at 19:35





@Aran-Fey, that's okay a good question(though asked before) can sometimes be resurrected for a fruitful discussion

– amanb
Apr 12 at 19:35




4




4





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

– juanpa.arrivillaga
Apr 12 at 19:55





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

– juanpa.arrivillaga
Apr 12 at 19:55




2




2





@juanpa.arrivillaga fair enough. Thanks for the explanation

– C.Nivs
Apr 12 at 21:19





@juanpa.arrivillaga fair enough. Thanks for the explanation

– C.Nivs
Apr 12 at 21:19












2 Answers
2






active

oldest

votes


















16














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




















  • 5





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

    – Jean-François Fabre
    Apr 12 at 19:27











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

    – amanb
    Apr 12 at 19:30











  • @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
    Apr 12 at 19:30






  • 6





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

    – Jean-François Fabre
    Apr 12 at 19:31











  • Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

    – drminix
    Apr 13 at 6:12



















6














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































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    16














    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




















    • 5





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

      – Jean-François Fabre
      Apr 12 at 19:27











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

      – amanb
      Apr 12 at 19:30











    • @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
      Apr 12 at 19:30






    • 6





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

      – Jean-François Fabre
      Apr 12 at 19:31











    • Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

      – drminix
      Apr 13 at 6:12
















    16














    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




















    • 5





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

      – Jean-François Fabre
      Apr 12 at 19:27











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

      – amanb
      Apr 12 at 19:30











    • @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
      Apr 12 at 19:30






    • 6





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

      – Jean-François Fabre
      Apr 12 at 19:31











    • Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

      – drminix
      Apr 13 at 6:12














    16












    16








    16







    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 Apr 12 at 19:28

























    answered Apr 12 at 19:25









    chepnerchepner

    264k36254346




    264k36254346







    • 5





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

      – Jean-François Fabre
      Apr 12 at 19:27











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

      – amanb
      Apr 12 at 19:30











    • @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
      Apr 12 at 19:30






    • 6





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

      – Jean-François Fabre
      Apr 12 at 19:31











    • Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

      – drminix
      Apr 13 at 6:12













    • 5





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

      – Jean-François Fabre
      Apr 12 at 19:27











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

      – amanb
      Apr 12 at 19:30











    • @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
      Apr 12 at 19:30






    • 6





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

      – Jean-François Fabre
      Apr 12 at 19:31











    • Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

      – drminix
      Apr 13 at 6:12








    5




    5





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

    – Jean-François Fabre
    Apr 12 at 19:27





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

    – Jean-François Fabre
    Apr 12 at 19:27













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

    – amanb
    Apr 12 at 19:30





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

    – amanb
    Apr 12 at 19:30













    @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
    Apr 12 at 19:30





    @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
    Apr 12 at 19:30




    6




    6





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

    – Jean-François Fabre
    Apr 12 at 19:31





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

    – Jean-François Fabre
    Apr 12 at 19:31













    Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

    – drminix
    Apr 13 at 6:12






    Thank you all! Coming from C/C++, I was just looking for a way to check if two different pointers point to the same object. So I should use "is operator" to compare if check if two pointers point to the same object. id() can return the same string since it can be re-used for transient objects. Thanks

    – drminix
    Apr 13 at 6:12














    6














    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





























      6














      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



























        6












        6








        6







        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 Apr 12 at 19:54

























        answered Apr 12 at 19:48









        hpauljhpaulj

        118k788162




        118k788162













            Popular posts from this blog

            រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

            Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

            Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221