[Python-ideas] with expression

Jan Kaliszewski zuo at chopin.edu.pl
Thu Feb 20 22:46:03 CET 2014


20.02.2014 22:33, Yann Kaiser wrote:

> The first one could be accomplished like:
>
>     x = [line.split() for line in f] with open(thisfile) as f
>
> It would keep f opened while the listcomp is being evaluated. This
> makes me think however of a likely accident:
>
>     x = (line.split() for line in f) with open('name') as f
>     next(x) # ValueError: I/O operation on closed file.
>
> This does mirror this mistake though:
>
>     with open('name') as f:
>         return (line.split() for line in f)
>
> When what was meant was:
>
>     with open('name') as f:
>         for line in f:
>             yield line.split()
>
> (Which is, unfortunately, a __del__ in disguise, which is frowned 
> upon.)

Why 'unfortunately'?

Cheers.
*j



More information about the Python-ideas mailing list