Re: context managers inline?

2016-03-11 Thread jmp
On 03/10/2016 07:59 PM, Neal Becker wrote: sohcahto...@gmail.com wrote: On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed?

Re: context managers inline?

2016-03-11 Thread Jussi Piitulainen
Paul Rubin writes: > Jussi Piitulainen writes: >> return ModeIO(f.read()) > > These suggestions involving f.read() assume the file contents are small > enough to reasonably slurp into memory. That's unlike the original > where "load" receives a stream and might process it piecewise. If y

Re: context managers inline?

2016-03-10 Thread Paul Rubin
Jussi Piitulainen writes: > return ModeIO(f.read()) These suggestions involving f.read() assume the file contents are small enough to reasonably slurp into memory. That's unlike the original where "load" receives a stream and might process it piecewise. -- https://mail.python.org/mailma

Re: context managers inline?

2016-03-10 Thread Jussi Piitulainen
Chris Angelico writes: > On Fri, Mar 11, 2016 at 5:33 AM, Neal Becker wrote: >> Is there a way to ensure resource cleanup with a construct such as: >> >> x = load (open ('my file', 'rb)) >> >> Is there a way to ensure this file gets closed? > > Yep! > > def read_file(fn, *a, **kw): > with ope

Re: context managers inline?

2016-03-10 Thread Steven D'Aprano
On Fri, 11 Mar 2016 05:33 am, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? Depends on what you mean by "ensure". Have load() call the file's close method may be g

Re: context managers inline?

2016-03-10 Thread Chris Angelico
On Fri, Mar 11, 2016 at 5:33 AM, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? Yep! def read_file(fn, *a, **kw): with open(fn, *a, **kw) as f: return f.

Re: context managers inline?

2016-03-10 Thread Mark Lawrence
On 10/03/2016 18:33, Neal Becker wrote: Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed? I don't see how there can be. Surely you must split it into two lines to use the context manager via

Re: context managers inline?

2016-03-10 Thread Ian Kelly
On Thu, Mar 10, 2016 at 11:59 AM, Neal Becker wrote: > sohcahto...@gmail.com wrote: > >> On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: >>> Is there a way to ensure resource cleanup with a construct such as: >>> >>> x = load (open ('my file', 'rb)) >>> >>> Is there a way to e

Re: context managers inline?

2016-03-10 Thread Neal Becker
sohcahto...@gmail.com wrote: > On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: >> Is there a way to ensure resource cleanup with a construct such as: >> >> x = load (open ('my file', 'rb)) >> >> Is there a way to ensure this file gets closed? > > with open('my file', 'rb')

Re: context managers inline?

2016-03-10 Thread sohcahtoa82
On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? with open('my file', 'rb') as f: x = load(f) -- https://mail.p

context managers inline?

2016-03-10 Thread Neal Becker
Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed? -- https://mail.python.org/mailman/listinfo/python-list