Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'else:': 0.03; 'source,': 0.04; 'none:': 0.07; 'methods,': 0.09; 'none)': 0.09; 'try:': 0.09; 'api,': 0.16; 'expecting': 0.16; 'file-like': 0.16; 'iterator': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'mon,': 0.24; 'pass': 0.26; 'subject:/': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "d'aprano": 0.31; 'object.': 0.31; 'steven': 0.31; 'file': 0.32; 'probably': 0.32; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'black': 0.61; 'close': 0.67; 'mar': 0.68; '2015': 0.84; 'order:': 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 :content-type; bh=m0AYy9jV1a1Rt0gwDaqx9PpIDpc3xnlxK1HQ144i3ek=; b=lL+Fhhy9Xh1/YLKK/VkEbI/eUX+rPx5vQGO46x77I7ADQYxGO3xB1NLnHiFq4OcCSv dsUWS0aqoglj7yAn2Se/65f/vkcZVqpxeh4SmTSpDhxqZGkVEgGNVVokcriBb0A62uEO wlyvynFUP3PmKt3633hUd3+YbLmzCf1L1DnrWYNYFcE2v03aDZWbbcBR8kuIqB806TpA AiRMHlYqi5at0dRbwhHzJOIUWvtOa7P5THbFVk2v6SPZL2Za/kMdeNBeoA0iVFpXPGK9 p4APMtYWSDrexfwvYyfg68YmZWdYkVX17xnkLvTosKegdKTrYeJBK6fUMD1M5uxv2wKV qVJQ== X-Received: by 10.70.65.39 with SMTP id u7mr78229539pds.11.1426517148430; Mon, 16 Mar 2015 07:45:48 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <5506e96b$0$13009$c3e8da3$5496439d@news.astraweb.com> References: <5501be8b$0$13006$c3e8da3$5496439d@news.astraweb.com> <874mpnp6nv.fsf@elektro.pacujo.net> <87y4mznmj8.fsf@elektro.pacujo.net> <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com> <87egopmn4z.fsf@elektro.pacujo.net> <55069623$0$12901$c3e8da3$5496439d@news.astraweb.com> <87vbi1qg0t.fsf@elektro.pacujo.net> <7cf04117-0fd5-40fb-bcaf-351353afc68b@googlegroups.com> <87lhixqe4p.fsf@elektro.pacujo.net> <5506e96b$0$13009$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Mon, 16 Mar 2015 08:45:08 -0600 Subject: Re: generator/coroutine terminology To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426517151 news.xs4all.nl 2831 [2001:888:2000:d::a6]:54594 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87565 On Mon, Mar 16, 2015 at 8:32 AM, Steven D'Aprano wrote: > On Tue, 17 Mar 2015 12:13 am, Marko Rauhamaa wrote: > >> If I get an iterator from a black box source, I don't know if I'm >> allowed/supposed to call close() on it. > > In no particular order: > > #1 > if hasattr(some_iterator, 'close'): > some_iterator.close() > > > #2 > close = getattr(some_iterator, 'close', None) > if close is not None: > close() > > > #3 > try: > close = some_iterator.close > except AttributeError: > pass > else: > close() Note that these fail if some_iterator is a file-like object. Those have close methods, but as part of the file api, not the iterator api, and so the owner of the object is probably not expecting you to close it for them.