[Tutor] A deeper explanation of ability to modify list elements in-place

Serdar Tumgoren tumgorenz at washpost.com
Thu Nov 11 20:21:22 CET 2010


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 :)


> Why? Because the designer of Python, Guido van Rossum, wanted it to be
> possible, and he designed the language so that it would be.
>
> Do you perhaps mean *how* is it possible?
>
Yes indeed, I suppose that is really what I was asking.


>
> The use of pointers is an implementation detail for efficiency, and it
> refers to how Python's object model is implemented in C.
>
> 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.
>
> In Python, if you have x[0] = Dummy(), the list object x stores the Dummy
> instance itself.


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.

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.

And that seems to explain why the Dummy instance remains accessible and is
not garbage-collected. Is that a correct understanding?


> 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.



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.


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.


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101111/c82423e5/attachment.html>


More information about the Tutor mailing list