Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; ';-)': 0.03; 'context': 0.07; 'explicit': 0.07; 'thrown': 0.09; 'cc:addr:python-list': 0.11; 'already,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'unwinds': 0.16; 'subject:python': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'skip': 0.24; 'cc:2**0': 0.24; 'header :In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'concise': 0.31; 'not.': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'manager': 0.38; 'pm,': 0.38; 'even': 0.60; 'skip:o 30': 0.61; 'myself': 0.63; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'close': 0.67; 'honest': 0.78; "'with'": 0.84; '2015': 0.84; 'to:none': 0.92 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 :content-type; bh=PFPIh7hk1q6/QeKKKgZJrKhjDxsJ6otZqFDATJr7Y20=; b=yM8rms5Mo7PhAWIjw5kFlGbhsRifOi0IWBWVSiO1dW61uHwMx7ib2TrfnaGXBueiXw j+MClHl2u10Mn2oH8yCyxKFRvROG58sxEa92bgHY2WX+tzn8SkmF/o6Rc62nL7ZzVI/Z uzZVcbbpciND50yYvDk3DGwCFRQg6Pmka1RQ8RXdl4cUsCYtkZFWZeYSaL+oG94UrXcV DykS3qxSg/busO0NWtGMsx1y05mKW6Dm1Oj6WPWDP4E17rI5nDDlDUGplqDIf2K/vj0O 2JT/oA5fRiHz+9IrYpcJgddToe5ssXpCPKhMAQ328jxubNkc1on0BsSX09IrWvcsUIXp QNxw== MIME-Version: 1.0 X-Received: by 10.50.114.35 with SMTP id jd3mr2368871igb.14.1430386750785; Thu, 30 Apr 2015 02:39:10 -0700 (PDT) In-Reply-To: <87twvyvvct.fsf@Equus.decebal.nl> References: <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> <87vbge9lm6.fsf@Equus.decebal.nl> <87y4lavy4q.fsf@Equus.decebal.nl> <87twvyvvct.fsf@Equus.decebal.nl> Date: Thu, 30 Apr 2015 19:39:10 +1000 Subject: Re: seek operation in python From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430387214 news.xs4all.nl 2888 [2001:888:2000:d::a6]:42548 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89618 On Thu, Apr 30, 2015 at 7:06 PM, Cecil Westerhof wrote: > I already done it. I thought it not to much work. And it even makes > some code shorter: > - marshal_file = open(expanduser(marshal_filename), 'r') > - not_list = load(marshal_file) > - marshal_file.close() > - return not_list > + with open(expanduser(marshal_filename), 'r') as f: > + return load(f) > > But here I did the close myself already, so that is not completely > honest of me. ;-) The context manager makes your code more concise AND more reliable - an exception thrown by load() will skip the explicit close(), but the 'with' block unwinds correctly whether there's an exception or not. ChrisA