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


Groups > comp.lang.python > #104543

Re: context managers inline?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: context managers inline?
Date Thu, 10 Mar 2016 12:19:19 -0700
Lines 23
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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de AK+RQDFTl3hKoKolLm5TYAri2vVs59wsggrnBgX7d9Ig==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.019
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'received:209.85.223': 0.03; 'cleanup': 0.07; "'rb')": 0.09; 'thursday,': 0.13; 'def': 0.13; 'thu,': 0.15; '11:59': 0.16; '2016': 0.16; 'readable': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:inline': 0.16; 'value:': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'load': 0.20; 'am,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'though.': 0.33; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'email addr:gmail.com': 0.62; 'march': 0.64; 'mar': 0.65; 'as:': 0.79; '(open': 0.84; 'becker': 0.84; 'construct': 0.84; 'func):': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=oekRE2d7sfUMfxOFTS73h8ytj/28zqeKORDJityLFFs=; b=dbHhRPcsu96ckvpI9XRbHn2BB11IfifxUlN89Ihp78OLRljvWhwV2C0uAFQoeq5I1q dLGDxBzSs4QY53R8Mt7Evw/MRIdIjPcVJyfp2HpaWa5m6osBvhpi2ruFagqaf8gjNq0C 5tksF1dzTbofrKilo5d1kMCqQL02yXwGoxHr44W9Mn6AbxkQFSfqahOZPV5k0dDFYyjU FOD/7YTQuXZ2Wnixldvh4tSw7sGypSWxMOxH/BTQjUZhHj/rJIWiM06UVcec5sg5pTj1 ctI3xk74x6o+8Wu55RXqIDAHFzxtL5TlXkMJSGtA3ihpcEGz7N3GqTqP5TUCKs4JFEW+ elYA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=oekRE2d7sfUMfxOFTS73h8ytj/28zqeKORDJityLFFs=; b=EKUmNXLfMjy0231X01vBO0kvdufdc3uX8qm70+e8ooRMCAQIX28+MnAIMGLZ+L/9mA LiN7z1r3yAS2Js2miKePTk0KXn6gEJawx5btw8IgTe48PCJbilqKJjmyhmSGEX4R1sdx nPDq9bozKrZopihytgseQGfVbsuP8/P39fs9dHEV64bEaYbUVftiELrM5wjKHa9o/XDR cmdioNdEtB4QkXpcaJz9iQynYI0pZwUL2B4hU8jYnNlZPkLEOW1ujHvNfaCOC7Bs3KTZ ko50Mveu4u6Jp1DLNPzDzD7jVq2Ovf4cxvZwerjBj1SaYSHrQklyC2AE+88klhKyYpAl XDxg==
X-Gm-Message-State AD7BkJIo/jhnOi0qocE2GOEDH6mdaQ/hPDH3rquNOb4eNSrgToOlEek86128Wz8mzP64GAY7H7GAJiAwtnLhSw==
X-Received by 10.107.19.140 with SMTP id 12mr6134772iot.11.1457637598895; Thu, 10 Mar 2016 11:19:58 -0800 (PST)
In-Reply-To <nbsg6k$c3o$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104543

Show key headers only | 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