Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'class,': 0.07; 'context': 0.07; 'subject:file': 0.07; 'cc:addr :python-list': 0.11; 'comparison.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'generator.': 0.16; 'iterator': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'helpful.': 0.31; 'shoot': 0.31; 'subject:some': 0.31; "we're": 0.32; 'run': 0.32; 'quite': 0.32; 'received:google.com': 0.35; 'raising': 0.36; 'manager': 0.38; 'nov': 0.38; 'question,': 0.38; 'list,': 0.38; 'how': 0.40; 'more': 0.64; 'subject:over': 0.84; 'subject:via': 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=YmQogkq+d+HvmvM/h2dhQeNRMGJkJLL+68Tfedjd2/w=; b=PV2h6lKctJAO8qGfUlkuaJIxhgsaPyLh7vQJ8vJYkRNUTIM3Nt7WwckxlPwTt771Jy oMrIORcVJWFDtAnjVuiUnU0KBLMy9fK5iS244ie6Uu3HDjvmilD6GfRN0qUhHPP4bmP7 hmzqk79389OuRRQw7hFnuub/80HRu1eYtsXApMiQVJlVrltDBo+9Rk0v7pKda/CvINt2 wbuNmUmQMSIJxmmXZ0tzis+CS9QMDM01XkiO//TymMyXjlbmbN0YlTJ17qnsyKUidcn9 NTjzeyhlk4pCytvLHbNh7XOHXq5csRlI39//zYHCVCY2eByeyAnTkXHNRRe3xP4vONpQ CUdQ== MIME-Version: 1.0 X-Received: by 10.42.64.143 with SMTP id g15mr35610752ici.59.1417197754491; Fri, 28 Nov 2014 10:02:34 -0800 (PST) In-Reply-To: <87fvd3dxb7.fsf@elektro.pacujo.net> References: <5478ab81$0$2879$426a34cc@news.free.fr> <87fvd3dxb7.fsf@elektro.pacujo.net> Date: Sat, 29 Nov 2014 05:02:34 +1100 Subject: Re: Iterate over text file, discarding some lines via context manager 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.15 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417197764 news.xs4all.nl 2927 [2001:888:2000:d::a6]:37527 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81699 On Sat, Nov 29, 2014 at 4:55 AM, Marko Rauhamaa wrote: > I think the OP can learn from the comparison. One question, many > lessons: > > * Here's how you write a generator. > > * Here's how you write a context manager class. You run into those > quite often as well. > > * See how much more suitable a generator is in this case. > > No need to shoot down those who only try to be helpful. Since we're so good at it on this list, I will nit-pick: that's not a context manager class, that's an iterator class. A context manager has __enter__ and __exit__, an iterator has __iter__ (returning self) and __next__ (returning or raising StopIteration). ChrisA