Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'latter': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:iterable': 0.09; 'jan': 0.12; 'wrote': 0.14; 'random': 0.14; 'collections': 0.16; 'empty.': 0.16; 'iterable': 0.16; 'iterable,': 0.16; 'iterating': 0.16; 'iterator': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'wrote:': 0.18; 'meant': 0.20; '(the': 0.22; '>>>': 0.22; 'input': 0.22; 'header:User-Agent:1': 0.23; 'library,': 0.24; 'source': 0.25; 'class.': 0.26; 'order.': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; "skip:' 10": 0.31; 'yields': 0.31; 'class': 0.32; 'not.': 0.33; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'i.e.': 0.36; 'ordered': 0.36; 'set.': 0.36; 'method': 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'easily': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'number,': 0.60; 'tell': 0.60; 'no.': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'smith': 0.68; 'subject:there': 0.68; 'guaranteed': 0.75; 'received:fios.verizon.net': 0.84; 'subject:check': 0.84; 'yielded': 0.84; '"how': 0.91; 'do:': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Is there a canonical way to check whether an iterable is ordered? Date: Thu, 18 Sep 2014 09:46:29 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411048023 news.xs4all.nl 2849 [2001:888:2000:d::a6]:37018 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78016 On 9/18/2014 8:58 AM, Roy Smith wrote: > I suspect what he meant was "How can I tell if I'm iterating over an > ordered collection?", i.e. iterating over a list vs. iterating over a > set. One can check whether the iterable is a tuple, list, range, or tuple or list iterator (the latter not being re-iterable). >>> type(iter([])) >>> type(iter(())) > Is there anything which requires an iterator to be deterministic? No. An iterator can yields random number, input from a non-deterministic source -- human or mechanical, or items from a collection in shuffled order. Generator that do such can easily be turned into the __iter__ method of a class. > For example, let's say I have an iterable, i, and I do: > > list1 = [item for item in i] > list2 = [item for item in i] If i is an iterator or other non-reiterable, list2 will be empty. If i is an instance of a class with a non-deterministic __iter__ method, list2 will not necessarily be either empty or a copy of list1. > am I guaranteed that list1 == list2? Clearly not. > It will be for all the collections I can think of in the standard library, but if I wrote my own class with > an __iter__() which yielded the items in a non-deterministic order, > would I be violating something other than the principle of least > astonishment? There should not be any astonishment. 'Iterable' is a much broader category than 'deterministically re-iterable iterable'. -- Terry Jan Reedy