Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!novso.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subsequent': 0.05; 'subject:Python': 0.06; 'layers': 0.07; 'over,': 0.07; 'plenty': 0.07; 'skip:` 10': 0.07; 'arguments': 0.09; 'false,': 0.09; 'subject:using': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'args,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guarded': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'iterator,': 0.16; 'iterators': 0.16; 'operation,': 0.16; 'other,': 0.16; 'ought': 0.16; 'true:': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'passing': 0.19; '(the': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'earlier': 0.24; 'mon,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'equivalent': 0.26; 'this:': 0.26; 'pass': 0.26; 'least': 0.26; '(for': 0.26; 'supported': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'responded': 0.31; 'checking': 0.33; 'reader': 0.33; 'noticed': 0.34; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'possible': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'rather': 0.38; 'that,': 0.38; 'skip:. 10': 0.39; 'supporting': 0.39; 'how': 0.40; 'break': 0.61; 'making': 0.63; 'name': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'note:': 0.66; 'guaranteed': 0.75; 'batchelder': 0.84; 'comment.': 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=yYcKTgQyNImIwd/jkdfZpHa6O66klMNUSlvHhLGVTz8=; b=S/tgAoYYXDfZPkhD/q+5+mtsnTIBnq5bPiFumYUJDA09npgDe9cUmMLRlL0/Ahbc0H WI+6GC1Jdph6J1fJvo03/LlG3t4MHHpbHd5FWqaETXHZQQS7Pe9dVnE8QDQloDpOk2kg 3Tyt2d6BwaiZ1jMQIfuWL5oRpQNwYHGbQm+q8VP4Dl7IOjH0B+hau4ES3Hq2WGChmMOM VDl9AxBQ856aSdFywerIZqhvvs+DEEODtsGoKfZjcIRgCSIRGAp62FWQMgHCOilaIqK6 Ywt9Ojmf2BAV5jXtUcnoS3s1Beu9DZZMBl/SyLo+y5natkrqz6nMAYcpvIz3E5deYwxM 8uVA== MIME-Version: 1.0 X-Received: by 10.42.94.14 with SMTP id z14mr15680251icm.59.1417976529057; Sun, 07 Dec 2014 10:22:09 -0800 (PST) In-Reply-To: References: <004e01d01173$e1641c10$a42c5430$@gmail.com> Date: Mon, 8 Dec 2014 05:22:08 +1100 Subject: Re: Python Iterables struggling using map() built-in 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417976537 news.xs4all.nl 2850 [2001:888:2000:d::a6]:41051 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82074 On Mon, Dec 8, 2014 at 2:29 AM, Ned Batchelder wrote: > 3. The only operation supported on iterators is next(). You cannot start > them over, you cannot ask if there will be more values, you cannot find out > how many values there will be, you can't ask what the last value was, etc. > By supporting only one operation, they allow the broadest possible set of > implementations. Technically, this is one of only two operations *guaranteed to be* supported on iterators (the other being that `iter(iterator) is iterator`). There are plenty of iterators which do more than that, but all iterators are guaranteed to support next() and nothing more. (For instance, a generator object is an iterator, and it supports a lot more operations.) > 5. The "for NAME in EXPR" construct is equivalent to this: > > expr_iter = iter(EXPR) > try: > while True: > NAME = next(expr_iter) > ..DO_SOMETHING.. > except StopIteration: > pass Small subtlety: The body of the for block is _not_ guarded by the try/except. It's more like this: expr_iter = iter(EXPR) while True: try: NAME = next(expr_iter) except StopIteration: break ..DO_SOMETHING.. > NOTE: THIS EXAMPLE IS HORRIBLE. This code is crazy-confusing, and should > never have been used as an example of iteration. It layers at least three > iterations on top of each other, making it very difficult to see what is > going on. It uses "while iters" where "while True" would do exactly the > same thing (iters will never be false). There's one way for iters to be false, and that's if you give it no arguments at all. I've only just noticed this now, as I responded earlier with a suggestion to try passing it no args, which won't work because of that (or at least, won't work in Py2; in Py3, iters will indeed never be false, unless you use list() to coalesce the map). This is something which definitely ought to have been given a comment. Or, more usefully, a guarding 'if' before the loop, rather than needlessly checking at every iteration - if you want an infinite loop guarded by a precondition, write it as such so the subsequent reader can see that that's your intention. ChrisA