[Python-ideas] for line in input with open(path) as input...

Yuval Greenfield ubershmekel at gmail.com
Sun Feb 3 09:38:33 CET 2013


On Sun, Feb 3, 2013 at 6:26 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> If you need both files open at the same time, you can use a nested
>  context manager:
>
>     def f():
>         with open(path) as fhand:
>             with open(path2) as ghand:
>                 process(fhand, ghand)
>
> Or the nesting behaviour built into with statements themselves:
>
>     def f():
>         with open(path) as fhand, open(path2) as ghand:
>             process(fhand, ghand)
>
>
This is indeed what I was looking for. The "with" statement does give a lot
of control, though I do dislike the double indentation and also dislike
opening 2 files on one line. Sucks to be me.


> Function and class definitions control name scope (amongst other
> things), with statements control deterministic cleanup, loops control
> iteration. That's what I mean by "separation of concerns" in relation
> to these aspects of the language design and it's a *good* thing (and
> one of the key reasons with statements behave like PEP 343, rather
> than being closer to Guido's original looping idea that is described
> in PEP 340).
>
>
Yes, separation of concerns is indeed a good thing and as Guido mentioned,
there are already too many ways to do this.

Thanks for the enlightenment,

Yuval
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130203/cebad62e/attachment.html>


More information about the Python-ideas mailing list