Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: context managers inline? Date: Thu, 10 Mar 2016 19:45:38 +0000 Lines: 18 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de aC4ttRDetfP1g1GYBBuCdAAejkxolR968rYRjZ1eBcyw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'context': 0.05; 'from:addr:yahoo.co.uk': 0.05; 'cleanup': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'keyword,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:inline': 0.16; 'wrote:': 0.16; 'language': 0.19; 'load': 0.20; 'lawrence': 0.22; 'leave': 0.23; 'split': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'language.': 0.32; 'surely': 0.33; 'file': 0.34; 'gets': 0.35; 'there': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'mark': 0.40; 'charset:windows-1252': 0.62; 'our': 0.64; 'as:': 0.79; "'with'": 0.84; '(open': 0.84; 'becker': 0.84; 'construct': 0.84; 'pythonistas,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.129.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104547 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 the 'with' keyword, or you leave the one line as is and forego the context manager. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence