Paul Rubin wrote:
>>>lines = file("myfile","r").readlines()
>
> It's released even if the exception is raised inside readlines?
I think so, but only because readlines is a builtin function.
If it wasn't, there would be a stack frame for readlines, which
would have "self" as a local variable.
As
"Martin v. Löwis" <[EMAIL PROTECTED]> writes:
> > 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 throu
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
Martin v. Löwis wrote:
> Paul Rubin wrote:
> >>Consider the function above. Do I need the fp.close(), or will the
> >>file be closed automatically when fp goes out of scope and its
> >>reference count drops to zero?
> >
> > In CPython, fp gets closed when it leaves scope.
>
> One issue is that wh
Paul Rubin wrote:
>>Consider the function above. Do I need the fp.close(), or will the
>>file be closed automatically when fp goes out of scope and its
>>reference count drops to zero?
>
> In CPython, fp gets closed when it leaves scope.
One issue is that when the function terminates through an
John Reese <[EMAIL PROTECTED]> writes:
> Consider the function above. Do I need the fp.close(), or will the
> file be closed automatically when fp goes out of scope and its
> reference count drops to zero?
In CPython, fp gets closed when it leaves scope. In other
implementations you may need try
def uselessHash(filename):
fp= open(filename)
hash= 0
for line in fp:
hash ^= hash(line.strip())
fp.close() # do I need this or is fp closed by ref count?
return hash
Consider the function above. Do I need the fp.close(), or will the
file be closed automatically when fp goes out