On Fri, 20 Nov 2015 05:50 am, BartC wrote:
> I said that Python's "=" does a very shallow copy. And I stated that in > A=B, something of B must be copied into A. > > I (and probably others) would like to know why none of that is correct. > But I suspect I'm not wrong. Nothing of B is copied. Here is a terrible ASCII art diagram of an object, floating in memory (most likely in the heap), an instant before the garbage collector can reach it and collect it: +----------------------+ | DEADBEEF0012345... | +----------------------+ (Best viewed in a monospaced font, like Courier.) The contents of the object are shown as a hex-dump, and will depend on the actual object and its value, but in general you would expect a field which acts as some sort of tag or pointer linking the instance to its parent class, plus other fields specifying the object's value and/or attributes. The internal details of the object don't matter, but basically objects are structs or records. Here is an ASCII art diagram of that same object, now bound to the name "B", preventing the GC from collecting it: +----------------------+ B------------->| DEADBEEF0012345... | +----------------------+ The name "B" may be on the heap, or in an array. It is possible that the human-readable string "B" itself no longer appears in memory. I don't care about the specific implementation, but for the record, in the case of CPython the name "B" is itself a string object in the heap, linked to from a specific associative array (hash table) also in the heap. (I think.) The link between the name B and the object ---> could be anything that allows the compiler/interpreter to associate one to the other. We call it a "reference". In CPython, references are pointers. If somebody were to write Python using FORTRAN 77, they would most likely use a big array to hold the objects, and use the index into the array as references. Other implementations may use other things, but for simplicity, let's just stick with calling it a pointer. Now let's do the assignment A = B: +----------------------+ B------------->| DEADBEEF0012345... | +->+----------------------+ | A-----------+ This adds a new reference (think: pointer) to the object, linked to the name "A". But **nothing of the object** is copied. The pointers to the object are not part of the object -- they are external to the object. If you want to talk about anything being copied, you should talk about *copying the reference* to B, not copying B. [Aside: there is some ambiguity here. If I say "a reference to B", I actually mean a reference to the object referenced to by B. I don't mean a reference to the *name* B. Python doesn't support that feature: names are not values in Python.] -- Steven -- https://mail.python.org/mailman/listinfo/python-list