Hacker News new | past | comments | ask | show | jobs | submit login

You can do multiple in a single with statement these days.



This is an often overlooked feature of context managers; to clarify, this is a perfectly normal example:

    with open("foo.txt") as foo_file, open("bar.txt") as bar_file, open("baz.txt", "w") as baz_file:
        baz_file.write(foo_file.read() + bar_file.read())


I think you can also do something like this if you want it to be readable

    with (open("foo.txt") as foo_file,
          open("bar.txt") as bar_file,
          open("baz.txt", "w") as baz_file):
        baz_file.write(foo_file.read() + bar_file.read())




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: