Reference to list element question

Fredrik Lundh effbot at telia.com
Wed Feb 16 14:40:33 EST 2000


David R. Favor <dfavor at austin.ibm.com> wrote:
> The following code attempts to create a reference to a
> list element to reduce overhead for large files, however
> the result is a copy operation.
>
> How does one create a pointer, or reference, to a list
> element, such that the list element data may be operated
> upon via the pointer (reference)?

in python, everything is a reference.

but that doesn't help you in this case, since

a) strings cannot be modified in place, and
b) assignment doesn't work as you think

or in other words,

after the line "line = lines[i]", line points to
to the i'th list item.

however, "line[:-1]" creates a new object
containing the substring, and "line = line[:-1]"
modifies the "line" variable to point to the *new*
object.  "lines[i]" isn't affected by this.

(hint: in this case, the "map" function might
be what you need.  see the library reference
for details)

</F>





More information about the Python-list mailing list