Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104607

Re: context managers inline?

From jmp <jeanmichel@sequans.com>
Newsgroups comp.lang.python
Subject Re: context managers inline?
Date 2016-03-11 11:42 +0100
Message-ID <mailman.13.1457693002.26429.python-list@python.org> (permalink)
References <mailman.144.1457634813.15725.python-list@python.org> <5474fede-5093-4a0b-8e1e-22737e6e7810@googlegroups.com> <nbsg6k$c3o$1@ger.gmane.org>

Show all headers | View raw


On 03/10/2016 07:59 PM, Neal Becker wrote:
> sohcahtoa82@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') as f:
>>      x = load(f)
>
> But not in a 1-line, composable manner?
>

You you really want a 1 liner

with open('my file', 'rb') as f: print 'foo'; x = load(f)


jm

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

context managers inline? Neal Becker <ndbecker2@gmail.com> - 2016-03-10 13:33 -0500
  Re: context managers inline? sohcahtoa82@gmail.com - 2016-03-10 10:47 -0800
    Re: context managers inline? Neal Becker <ndbecker2@gmail.com> - 2016-03-10 13:59 -0500
    Re: context managers inline? Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-10 12:19 -0700
    Re: context managers inline? jmp <jeanmichel@sequans.com> - 2016-03-11 11:42 +0100
  Re: context managers inline? Steven D'Aprano <steve@pearwood.info> - 2016-03-11 15:35 +1100

csiph-web