[Tutor] Iterating through a list of strings
Stefan Behnel
stefan_ml at behnel.de
Mon May 3 13:08:18 CEST 2010
Luke Paireepinart, 03.05.2010 12:18:
> On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote:
>> Luke Paireepinart, 03.05.2010 10:27:
>>> I thought they changed for loop interations so that if you did
>>> for line in open('packages.txt'):
>>> .... etc...
>>>
>>> it would automatically close the file handle after the loop terminated.
>>> Have I been wrong this whole time?
>>
>> Yes. The fact that the file is automatically closed after the loop is an
>> implementation detail of CPython that does not apply in other Python
>> implementations.
>
> So why is it an implementation detail? Why is it not universally like that?
Because it cannot be done like this without implementation details that are
inherent to the CPython interpreter, namely: reference counting. The
for-loop cannot know where the object reference came from that it is just
trying to iterate over. Only after the for-loop dropped the reference, the
reference counting mechanism determines that it was the last reference and
cleans up the file. When the file is cleaned up, the code in the file
object determines that it wasn't closed yet and closes the file. Neither
the for-loop, nor the reference-counting mechanism, nor the garbage
collector know that this is something that needs to be done. Only the file
object knows that.
> You never have an explicit reference to the file handle. When it gets
> garbage-collected after the loop it should get rid of the file handle.
But you cannot know when that will be. It is not an implementation detail
that the file gets closed when the file object is freed. It *is*, however,
an implementation detail, when this freeing happens. It can take an
arbitrarily long time, so it is usually in your own interest to close the
file yourself, if only to get rid of the file handle. Imagine you had
opened the file for writing, such as in
open("myfile.txt", 'w').write('some text')
Here, you have no guarantee that the text has been written to the file at
any point in the runtime of your program. Likely not what you want.
> I mean, where is the line between 'implementation details' and
> 'language features'?
See the docs.
> What reason is there to make lists mutable but
> strings immutable?
Performance considerations and use case specific design.
> Why aren't strings mutable, or lists immutable?
What would be the use case of an immutable list, as opposed to a tuple? How
would you use mutable strings in a dictionary?
Stefan
More information about the Tutor
mailing list