First off, thanks for the detailed response! I had a few follow-up questions below, if you're willing to indulge. Feel free to direct me to RTM some books on C and the python source code, of course :)<br><br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">
<br></div>
Why? Because the designer of Python, Guido van Rossum, wanted it to be possible, and he designed the language so that it would be.<br>
<br>
Do you perhaps mean *how* is it possible?<br></blockquote><div>Yes indeed, I suppose that is really what I was asking. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">
<br></div>
The use of pointers is an implementation detail for efficiency, and it refers to how Python's object model is implemented in C.<br>
<br>
It is important not to lose sight of the distinction between the semantics of Python objects themselves, and the way that those objects are implemented in lower-level languages such as C.<br>
<br>
In Python, if you have x[0] = Dummy(), the list object x stores the Dummy instance itself. </blockquote><div><br></div><div>I think the above example gets to the source of my confusion. Clearly the instance remains accessible via x[0], but I somehow never thought of a specific list index as an obvious "symbol" for -- or reference to -- an instance. I suspect that my confusion stems from the day-to-day routine of instantiating an object and assigning it to a variable at the same time, often inside a loop, and then performing additional processing on this instance *before* storing it in a list or some other container.</div>
<div><br></div><div>Above, it seems that you're *still* performing simultaneous instantiation and assignment (from a code perspective), but the "symbol" that you're assigning to is the list index itself.</div>
<div><br></div><div>And that seems to explain why the Dummy instance remains accessible and is not garbage-collected. Is that a correct understanding?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
That's what the code *means* -- create an instance of the Dummy class and store it in the list x. But of course for efficiency reasons, and ease of programming, the most obvious implementation will be to place the instance in the heap somewhere, and keep a pointer to it in the list. How else could you do it? You could store the entire object in the list, but that would be terribly inefficient. </blockquote>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
But it could be done -- you could, with difficulty, implement Python in a language like Fortran 77 (which has no dynamic memory allocation, and therefore no pointers), or in Haskell, which is purely functional. Or you could come up with a (slow, inefficient) implementation which moved around large instance objects instead of small pointers. </blockquote>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
There is no way to get access to the underlying pointers from pure Python code. From pure Python, pointers don't exist -- Python gives you no way to create or access pointers from Python code. All you have access to is objects.</blockquote>
<div><br></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>So is a reference simply a Python-level symbol (ie some collections of characters accessible from Python code) that corresponds to a pointer, which in turn points to the location in memory where the object is stored? </div>
</div>