reference counting and file objects

"Martin v. Löwis" martin at v.loewis.de
Tue May 24 15:26:02 EDT 2005


Peter wrote:

> Does the idiom:
> 
> lines = file("myfile","r").readlines()
> 
> have any better guarantee of being closed automatically? 

Yes. The file object only lives on the evaluation stack,
and that is discarded in any case when the function terminates
(whether through a return or through an exception). In
addition, the object is released as soon as readlines
returns.

Regards,
Martin



More information about the Python-list mailing list