how to count lines in a file ?

Alex Martelli aleax at aleax.it
Fri Jul 26 13:43:31 EDT 2002


On Friday 26 July 2002 07:08 pm, Bjorn Pettersen wrote:
	...
> If you make it a little more special purpose, I could propose syntax
> like e.g.:
>
>   let a = open('a'):
>       process(a)

That's basically what Microsoft proposes to add in some future C#
with the same semantics, except the keyword they have in mind is
'using' ('let' has strongly different connotations for VB users).

Not sure what you mean by "more special purpose", though.

But if you're opening and processing 3 files, it seems to me we're
back with the deep-nesting problem.

> being equivalent to:
>
>   a = None
>   try:
>      a = open('a')
>      process(a)
>   finally:
>      del a
>
> assuming a's __del__ was guaranteed to close the file, although it would

I think relying on a special method (__del__ or other) may be OK.  MS,
for their 'using', propose to rely on their finalizers, of course.

> probably be safer to create a new statement like:
>
>   close a
>
> which would call a.__close__ which would "do the right thing". Something
> like 'let' also works well with Python indentation.
>
> For a different approach you can look at Ruby which only has M/S GC, but
> also can guarantee file object lifetimes.

Guess I'll definitely have to check that -- I had missed this tidbit when
studying Ruby.


Alex




More information about the Python-list mailing list