[Python-ideas] With-expression

Gregory P. Smith greg at krypto.org
Tue Jan 20 02:47:56 CET 2009


On Mon, Jan 19, 2009 at 5:46 PM, Josiah Carlson <josiah.carlson at gmail.com>wrote:

> On Sun, Jan 18, 2009 at 2:41 PM, Leif Walsh <leif.walsh at gmail.com> wrote:
> > On Wed, Jan 14, 2009 at 2:12 AM, Carl Johnson <carl at carlsensei.com>
> wrote:
> >> From the department of doomed ideas:
> >>
> >>     data = f.read() with open("file.txt") as f
> >
> > "Who do you think we are, Ruby?"
> >
> > Just kidding, but -1.
>
> I always preferred:
>
> data = open("file.txt").read()
>
> Which works all the way back as far as I can remember.
>

In C Python yes that "works" but it makes absolutely no guarantees about
when that file is actually closed.  Don't write code that depends on a
particular GC behavior.  Use with. :)


>
> And as for:
> strippedLines = [sline for line in lines with line.strip() as sline if
> sline]
>
> Try:
> strippedLines = [sline for sline in (line.strip() for line in lines) if
> sline]
>
> The request gets a big fat -1 from me.
>
>  - Josiah
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090119/40022dc5/attachment.html>


More information about the Python-ideas mailing list