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


Groups > comp.lang.python > #104551

Re: context managers inline?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: context managers inline?
Date Fri, 11 Mar 2016 07:17:37 +1100
Lines 27
Message-ID <mailman.154.1457641060.15725.python-list@python.org> (permalink)
References <nbselg$he9$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de O6yUXlPu2Ynl5Vebfz8uDQA4X6MrPW7jBmJGK1G5Bbfg==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'context': 0.05; 'cleanup': 0.07; 'cc:addr:python-list': 0.09; 'cleanup,': 0.09; 'okay': 0.09; 'way:': 0.09; 'python': 0.10; '(at': 0.13; 'def': 0.13; '2016': 0.16; 'expression"': 0.16; 'f.read()': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:inline': 0.16; 'wrote:': 0.16; 'string,': 0.18; 'load': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'c++,': 0.22; 'function,': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'there.': 0.30; 'push': 0.30; 'statement': 0.32; 'rule': 0.33; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'closing': 0.36; 'form,': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'end': 0.39; 'does': 0.39; 'your': 0.60; 'entire': 0.61; 'mar': 0.65; 'effective.': 0.66; 'as:': 0.79; '(open': 0.84; '**kw)': 0.84; '**kw):': 0.84; 'becker': 0.84; 'chrisa': 0.84; 'construct': 0.84; 'returns.': 0.84; 'to:none': 0.91; 'safer': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=F8K2Ho9ex7jaarUpw6AAda4qyOmuZnkxnUtBHJP5kHQ=; b=RgqifK4AOYf8pV/8G8I/rOzjcVsbORgv/XJGlSpl4JR9ad3R+dk6ykQokrjdWCKzCD /89PqWKrGg37+YnHAZKe8pjj1VoYqT524MVhQH/UvuKDmOrIF/fXjg1O1eDqXp94ePJl dC5vR2jiOpkQjLeQpZmUBL3MLi+C/Jelzh/nf3LSuW4cWfGE32JlY+qXWNNcBuGYz2cR 2JZOH0q5HOSC+Axm10CQEXDtm3NqdLWTS87OsQWQGG/+UqCMC+hl9jgWGhwDfqmnH7jF X0QI79Jim8USteJIDY/+KBe123LG3d1uDoq6IphSp5vWlVDMYTJwqegQ782/1LwjqJVq CFmQ==
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:date :message-id:subject:from:cc; bh=F8K2Ho9ex7jaarUpw6AAda4qyOmuZnkxnUtBHJP5kHQ=; b=h5XdQ7h6ZIZe/AsEQaSry3mq2lIsBWjJR4SNNkpeqwgjHZ7+NJ7qYBHUBg2L/nOj3C 7knCTp7a0c5x0m5wkO7l4CwXz6tNH4FoJy3O9FQop6Q0P3BiRDqIgCoa7xi13CG2ORKX 5G5LSQGyv3IB/fyj7VUf9VzBk3dph/cPHcKJZq553iVzYOgjK8kXrPgHDfekLpo7bNZQ fvU7Ezjqms6IvxUKJh48isFZ0VhDTJQXUhcctJ6h5vLJmMJkgVRQ57G18Yy1AdPiK0R6 Y7Wybe7lAGd1M1YBpJcmXDG5lOYU7C1R7g8nGgCdyrTw2YxOaJeitRckCCx5VH5y9oAV RiTg==
X-Gm-Message-State AD7BkJJ3Nx91gBvSXpzlj6oqGud6kVsNgCT2PjxieqR7TmGS6ozYtpX0wGuBcxzXaHVZvkFnt56I0cjnu+I/zQ==
X-Received by 10.50.28.105 with SMTP id a9mr244153igh.94.1457641057169; Thu, 10 Mar 2016 12:17:37 -0800 (PST)
In-Reply-To <nbselg$he9$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:104551

Show key headers only | View raw


On Fri, Mar 11, 2016 at 5:33 AM, Neal Becker <ndbecker2@gmail.com> 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.read()

Now you can ensure resource cleanup, because the entire file has been
read in before the function returns. As long as your load() function
is okay with reading from a string, this is effective.

Alternatively, push the closing the other way: pass a file name to
your load() function, and have the context manager in there.

If you don't do it one of those ways, the question is: WHEN should the
file be closed? How does Python know when it should go and clean that
up? There's no "end of current expression" rule as there is in C++, so
it's safer to use the statement form, which has a definite end (at the
unindent).

ChrisA

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


Thread

Re: context managers inline? Chris Angelico <rosuav@gmail.com> - 2016-03-11 07:17 +1100
  Re: context managers inline? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-11 08:09 +0200
    Re: context managers inline? Paul Rubin <no.email@nospam.invalid> - 2016-03-10 23:30 -0800
      Re: context managers inline? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-11 11:16 +0200

csiph-web