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


Groups > comp.lang.python > #104543

Re: context managers inline?

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: context managers inline?
Date 2016-03-10 12:19 -0700
Message-ID <mailman.147.1457637602.15725.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 Thu, Mar 10, 2016 at 11:59 AM, Neal Becker <ndbecker2@gmail.com> 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?

def with_(ctx, func):
    with ctx as value:
        return func(value)

x = with_(open('my file', 'rb'), load)


Seems less readable to me, though.

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