2

Why is the assignment operator is not making a copy of the rvalue but a reference to that(on a list), and you have to use slices in order to make a real copy which creates an independent object so that changes on one does not affect the other. Is this related to some specific usage related the language that I missed until now?

Edit: what I understod is that in C++

int a = 1;
int b = a;
b = 2;    // does not affect a 

so I also thought that would be the same reasoning since Python is developed in C and it takes care of it with pointers most probably. With some simple code:

int a = 1; 
/*int b = a;*/
int &b = a; /* what python does as I understood, if so why it does that this way?*/ 

is that more clear?

What I asked was more a comparison question on which I should be more clear, I agree ;-)

9
  • 2
    Please read up on "mutable" structures. Lists (and dictionaries) are special because their mutable. After reading up on "mutable", please update your question to be more specific. Commented Apr 7, 2011 at 11:48
  • 1
    S. Lott speaks truth and breathes justice. Commented Apr 7, 2011 at 11:52
  • Variables in Python are references to objects. That's how it's designed. If slicing feels ugly, there's the copy module. Commented Apr 7, 2011 at 11:55
  • 1
    @S.Lott, what does it have to with mutability? Tuples are the same way and they're immutable. Commented Apr 7, 2011 at 12:08
  • @cool-RR: Depends on what part of the question you're interested in. "Why is the assignment operator is not making a copy" is one thing. "you have to use slices in order to make a real copy" is another thing. And "Is this related to some specific usage related the language that I missed until now?" is largely unanswerable noise. So, I figured that a little reading might help clarify what the real question is. Commented Apr 7, 2011 at 12:22

2 Answers 2

2

I recently posted an answer that discusses exactly this issue.

Sign up to request clarification or add additional context in comments.

4 Comments

nice link and good explanation, my understanding is more or less correct so I am happy... +1 for that
@Umut Tabak: Under the answer is a "tick mark" that you can click to show your happiness.
Reading a bit more about the classes, I guess this reference isssue is related to garbage collection that is automatic in python, so that for 'free'ing these pointers automatically, maybe not complete but I guess right reasoning
Well, when the number of references owned to an object drops to zero, it can get "collected". So in that sense, what you're saying is correct.
2

In python, everything can be considered as a reference. If you want an assignment to be a copy, you always need to put it excplicitely in your expression

a = range(10) # create a list and assign it to "a"
b = a  # assign the object referenced by "a" to b
a is b # True --> a and b are two references to the same object. Works with any object 
       # after b = a
from copy import deepcopy
c = deepcopy(a) # or c = a[:] for lists
c is a # False

2 Comments

An object is a named storage and the name is how you refer to the memory location, this is what I know. From this perspective, a = [1,2,3] , so [1,2,3] is the real object that is stored in memory and a is the name of the container that holds this object, is that right, first of all? What I was thinking was that assignment is making the copy automatically but apparently it does not work that way...
no it doesn't. "b = a" only gives a new name to an object stored in memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.