Python memory allocation












2















I am really interested how Python memory allocation works.



I have written 2 pieces of code:



1.



list = 
a = [1,2,3]
list.append(a)
a = [3,2,1]
print(list)
print(a)


2.



def change(list):
list[1] = 50
list[2] = 70

list =
a = [1,2,3]
list.append(a)
change(a)
print(list)
print(a)


When I compile the first code i get result [[1,2,3]] and [3,2,1].



However, when I compile the second code the result I get is [1,50,70].



In the first case I create an object "a" and an array "list". When I append my object "a" to the array, array points at it in fact. Then, when I am assigning "a" new array [3,2,1], the object [1,2,3] is saved in the array and "a" points at new array [3,2,1].



In the second case I also create an object "a" and an array "list". I append "a" to array and there's also a pointer, that points at "a". Then I'm calling a method on an array "a". After calling the function and changing the value of the elements, array "list" still points at object "a" without creating a new instance.



Can somebody explain me the way it really works?










share|improve this question


















  • 1





    Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

    – user2357112
    Nov 26 '18 at 23:54











  • Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

    – dyukha
    Nov 26 '18 at 23:56











  • Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

    – juanpa.arrivillaga
    Nov 27 '18 at 0:33


















2















I am really interested how Python memory allocation works.



I have written 2 pieces of code:



1.



list = 
a = [1,2,3]
list.append(a)
a = [3,2,1]
print(list)
print(a)


2.



def change(list):
list[1] = 50
list[2] = 70

list =
a = [1,2,3]
list.append(a)
change(a)
print(list)
print(a)


When I compile the first code i get result [[1,2,3]] and [3,2,1].



However, when I compile the second code the result I get is [1,50,70].



In the first case I create an object "a" and an array "list". When I append my object "a" to the array, array points at it in fact. Then, when I am assigning "a" new array [3,2,1], the object [1,2,3] is saved in the array and "a" points at new array [3,2,1].



In the second case I also create an object "a" and an array "list". I append "a" to array and there's also a pointer, that points at "a". Then I'm calling a method on an array "a". After calling the function and changing the value of the elements, array "list" still points at object "a" without creating a new instance.



Can somebody explain me the way it really works?










share|improve this question


















  • 1





    Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

    – user2357112
    Nov 26 '18 at 23:54











  • Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

    – dyukha
    Nov 26 '18 at 23:56











  • Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

    – juanpa.arrivillaga
    Nov 27 '18 at 0:33
















2












2








2


1






I am really interested how Python memory allocation works.



I have written 2 pieces of code:



1.



list = 
a = [1,2,3]
list.append(a)
a = [3,2,1]
print(list)
print(a)


2.



def change(list):
list[1] = 50
list[2] = 70

list =
a = [1,2,3]
list.append(a)
change(a)
print(list)
print(a)


When I compile the first code i get result [[1,2,3]] and [3,2,1].



However, when I compile the second code the result I get is [1,50,70].



In the first case I create an object "a" and an array "list". When I append my object "a" to the array, array points at it in fact. Then, when I am assigning "a" new array [3,2,1], the object [1,2,3] is saved in the array and "a" points at new array [3,2,1].



In the second case I also create an object "a" and an array "list". I append "a" to array and there's also a pointer, that points at "a". Then I'm calling a method on an array "a". After calling the function and changing the value of the elements, array "list" still points at object "a" without creating a new instance.



Can somebody explain me the way it really works?










share|improve this question














I am really interested how Python memory allocation works.



I have written 2 pieces of code:



1.



list = 
a = [1,2,3]
list.append(a)
a = [3,2,1]
print(list)
print(a)


2.



def change(list):
list[1] = 50
list[2] = 70

list =
a = [1,2,3]
list.append(a)
change(a)
print(list)
print(a)


When I compile the first code i get result [[1,2,3]] and [3,2,1].



However, when I compile the second code the result I get is [1,50,70].



In the first case I create an object "a" and an array "list". When I append my object "a" to the array, array points at it in fact. Then, when I am assigning "a" new array [3,2,1], the object [1,2,3] is saved in the array and "a" points at new array [3,2,1].



In the second case I also create an object "a" and an array "list". I append "a" to array and there's also a pointer, that points at "a". Then I'm calling a method on an array "a". After calling the function and changing the value of the elements, array "list" still points at object "a" without creating a new instance.



Can somebody explain me the way it really works?







python pointers memory






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 23:49









coolchockcoolchock

132




132








  • 1





    Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

    – user2357112
    Nov 26 '18 at 23:54











  • Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

    – dyukha
    Nov 26 '18 at 23:56











  • Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

    – juanpa.arrivillaga
    Nov 27 '18 at 0:33
















  • 1





    Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

    – user2357112
    Nov 26 '18 at 23:54











  • Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

    – dyukha
    Nov 26 '18 at 23:56











  • Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

    – juanpa.arrivillaga
    Nov 27 '18 at 0:33










1




1





Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

– user2357112
Nov 26 '18 at 23:54





Memory allocation really isn't what you should be thinking about at this point. Read about how variables and objects interact first. Memory allocation is an implementation detail.

– user2357112
Nov 26 '18 at 23:54













Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

– dyukha
Nov 26 '18 at 23:56





Can you please clarify what exactly you don't understand? If I understood everything correctly, you already described how it works.

– dyukha
Nov 26 '18 at 23:56













Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

– juanpa.arrivillaga
Nov 27 '18 at 0:33







Those are not arrays, they are list objects. But in any event, please read the following: nedbatchelder.com/text/names.html

– juanpa.arrivillaga
Nov 27 '18 at 0:33














1 Answer
1






active

oldest

votes


















3














Every variable you create in Python is a reference to an object. Lists are objects which contain multiple references to different objects.



Almost all operations in python modify these references, not the objects. So when you do list[1] = 50 you are modifying the second reference that the list item contains.



I have found that a useful tool for visualising this is Python Tutor



So for your first example



list =  # you create a reference from `list` to the the new list object you have created
a = [1,2,3] # you create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `1`, `2` and `3`.
list.append(a) # this adds another reference to `list` to the object referenced by `a` which is the object `[1, 2, 3]`
a = [3,2,1] # create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `3`, `2` and `1`.
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`


For your second example



def change(list): # create a reference from `change` to a function object
list[1] = 50 #change the second reference in the object referenced by `list` to `50`
list[2] = 70 #change the third reference in the object referenced by `list` to `70`

list = # create a reference from `list` to a new list object
a = [1,2,3] # create a reference from `a` to a new list object which itself contains three references to three objects, `1`, `2` and `3`.
list.append(a) # add a reference to list to the object pointed to by `a`
change(a) # call the function referenced by change
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`





share|improve this answer





















  • 1





    That Python Tutor is worth several upvotes! Nice tool, really :-)

    – Alfe
    Nov 27 '18 at 0:00











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%2f53490783%2fpython-memory-allocation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Every variable you create in Python is a reference to an object. Lists are objects which contain multiple references to different objects.



Almost all operations in python modify these references, not the objects. So when you do list[1] = 50 you are modifying the second reference that the list item contains.



I have found that a useful tool for visualising this is Python Tutor



So for your first example



list =  # you create a reference from `list` to the the new list object you have created
a = [1,2,3] # you create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `1`, `2` and `3`.
list.append(a) # this adds another reference to `list` to the object referenced by `a` which is the object `[1, 2, 3]`
a = [3,2,1] # create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `3`, `2` and `1`.
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`


For your second example



def change(list): # create a reference from `change` to a function object
list[1] = 50 #change the second reference in the object referenced by `list` to `50`
list[2] = 70 #change the third reference in the object referenced by `list` to `70`

list = # create a reference from `list` to a new list object
a = [1,2,3] # create a reference from `a` to a new list object which itself contains three references to three objects, `1`, `2` and `3`.
list.append(a) # add a reference to list to the object pointed to by `a`
change(a) # call the function referenced by change
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`





share|improve this answer





















  • 1





    That Python Tutor is worth several upvotes! Nice tool, really :-)

    – Alfe
    Nov 27 '18 at 0:00
















3














Every variable you create in Python is a reference to an object. Lists are objects which contain multiple references to different objects.



Almost all operations in python modify these references, not the objects. So when you do list[1] = 50 you are modifying the second reference that the list item contains.



I have found that a useful tool for visualising this is Python Tutor



So for your first example



list =  # you create a reference from `list` to the the new list object you have created
a = [1,2,3] # you create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `1`, `2` and `3`.
list.append(a) # this adds another reference to `list` to the object referenced by `a` which is the object `[1, 2, 3]`
a = [3,2,1] # create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `3`, `2` and `1`.
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`


For your second example



def change(list): # create a reference from `change` to a function object
list[1] = 50 #change the second reference in the object referenced by `list` to `50`
list[2] = 70 #change the third reference in the object referenced by `list` to `70`

list = # create a reference from `list` to a new list object
a = [1,2,3] # create a reference from `a` to a new list object which itself contains three references to three objects, `1`, `2` and `3`.
list.append(a) # add a reference to list to the object pointed to by `a`
change(a) # call the function referenced by change
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`





share|improve this answer





















  • 1





    That Python Tutor is worth several upvotes! Nice tool, really :-)

    – Alfe
    Nov 27 '18 at 0:00














3












3








3







Every variable you create in Python is a reference to an object. Lists are objects which contain multiple references to different objects.



Almost all operations in python modify these references, not the objects. So when you do list[1] = 50 you are modifying the second reference that the list item contains.



I have found that a useful tool for visualising this is Python Tutor



So for your first example



list =  # you create a reference from `list` to the the new list object you have created
a = [1,2,3] # you create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `1`, `2` and `3`.
list.append(a) # this adds another reference to `list` to the object referenced by `a` which is the object `[1, 2, 3]`
a = [3,2,1] # create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `3`, `2` and `1`.
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`


For your second example



def change(list): # create a reference from `change` to a function object
list[1] = 50 #change the second reference in the object referenced by `list` to `50`
list[2] = 70 #change the third reference in the object referenced by `list` to `70`

list = # create a reference from `list` to a new list object
a = [1,2,3] # create a reference from `a` to a new list object which itself contains three references to three objects, `1`, `2` and `3`.
list.append(a) # add a reference to list to the object pointed to by `a`
change(a) # call the function referenced by change
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`





share|improve this answer















Every variable you create in Python is a reference to an object. Lists are objects which contain multiple references to different objects.



Almost all operations in python modify these references, not the objects. So when you do list[1] = 50 you are modifying the second reference that the list item contains.



I have found that a useful tool for visualising this is Python Tutor



So for your first example



list =  # you create a reference from `list` to the the new list object you have created
a = [1,2,3] # you create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `1`, `2` and `3`.
list.append(a) # this adds another reference to `list` to the object referenced by `a` which is the object `[1, 2, 3]`
a = [3,2,1] # create a reference from `a` to the new list you have created which itself contains 3 references to three objects, `3`, `2` and `1`.
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`


For your second example



def change(list): # create a reference from `change` to a function object
list[1] = 50 #change the second reference in the object referenced by `list` to `50`
list[2] = 70 #change the third reference in the object referenced by `list` to `70`

list = # create a reference from `list` to a new list object
a = [1,2,3] # create a reference from `a` to a new list object which itself contains three references to three objects, `1`, `2` and `3`.
list.append(a) # add a reference to list to the object pointed to by `a`
change(a) # call the function referenced by change
print(list) # print the object referenced by `list`
print(a) # print the object referenced by `a`






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 0:07

























answered Nov 26 '18 at 23:56









dangee1705dangee1705

2,0241722




2,0241722








  • 1





    That Python Tutor is worth several upvotes! Nice tool, really :-)

    – Alfe
    Nov 27 '18 at 0:00














  • 1





    That Python Tutor is worth several upvotes! Nice tool, really :-)

    – Alfe
    Nov 27 '18 at 0:00








1




1





That Python Tutor is worth several upvotes! Nice tool, really :-)

– Alfe
Nov 27 '18 at 0:00





That Python Tutor is worth several upvotes! Nice tool, really :-)

– Alfe
Nov 27 '18 at 0:00




















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%2f53490783%2fpython-memory-allocation%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

Lallio

Futebolista

Jornalista