Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'interpreter': 0.04; 'feature.': 0.07; 'iterate': 0.09; 'received :mail-vc0-f174.google.com': 0.09; 'subject:using': 0.09; 'def': 0.10; 'dec': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'iterators': 0.16; 'looping': 0.16; 'simpler,': 0.16; 'tighter': 0.16; 'used:': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'thu,': 0.17; 'examples': 0.18; '>>>': 0.18; 'not,': 0.21; 'keys': 0.22; 'skip:% 10': 0.22; 'example': 0.23; 'this:': 0.23; 'seems': 0.23; 'header:In-Reply- To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'possibility': 0.27; 'message-id:@mail.gmail.com': 0.27; "d'aprano": 0.29; 'directly,': 0.29; 'faster,': 0.29; 'received:209.85.220.174': 0.29; 'steven': 0.29; 'probably': 0.29; "i'm": 0.29; 'code': 0.31; 'could': 0.32; 'values.': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'optimization': 0.37; 'two': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'some': 0.38; 'gives': 0.39; 'performance': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'kind': 0.61; 'safe': 0.63; 'worth': 0.63; 'more': 0.63; "(don't": 0.84; "it'd": 0.84; 'items,': 0.91 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:to :content-type; bh=PVIS2S+DRl6ulN4SDfRKGIG7OJz7yiijPgMsy7ppv2s=; b=swSufylVbekL5Wghd+Kj02QaC8X2XaP8wYxNd/XJXkdCJljd+l9696Rr1lEr0/hgaM +q6SYuzNG/G/Ls5HjPsp/R9YyFu4fEzVgzyTlyJCxYTK/ZQnxic2PK4Xs8zslZm5znku 1u6sgNnD/fkfNvIuAWajy73i6/YeWBKcsAf/WeQt3UQ2mh5ToiFPfSp1PRtIOOe5t2m7 8fHOc5dzhQKqmEeSY6e/Stu2Yh3cfLA8x13cIHdkRNjwqU1yeK8w7dPDuXlldRtb7ZmB b09ERE7CMLy/sTzIO5EIryovkWZAnPOUvbtv5yhGcBz2+SAJWZ2ZSR8+tTnd4QjmQ8Sh U7pw== MIME-Version: 1.0 In-Reply-To: <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 13 Dec 2012 12:14:08 +1100 Subject: Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355361251 news.xs4all.nl 6842 [2001:888:2000:d::a6]:60684 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34746 On Thu, Dec 13, 2012 at 11:30 AM, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote: >> To me for i in range(len(l)) seems like simpler, faster, tighter code >> for this now. > > * the version with enumerate makes the intent more clear: since we > care about looping over the items, we should iterate over the > items directly, not over their indices; To add to this: Using enumerate gives the possibility (don't know if any current interpreter takes advantage or not, but a future one certainly could) that the enumerate() call could be optimized out. Yes, it's theoretically possible that someone could redefine enumerate, which would be broken by such an optimization. But all it'd take is some kind of directive-based optimization and voila, safe performance improvements. Example code used: >>> def foo(x): for i,val in enumerate(x): print("x[%d] = %s"%(i,str(val))) >>> def foo(x): for i in range(len(x)): val=x[i] print("x[%d] = %s"%(i,str(val))) A simple look at dis.dis() for the above two functions disproves the "faster". Steven has already disproven the "simpler" and "tighter". (I would like, though, to see a key-providing iteration as a standard feature. Looking at dis.dis() for the above examples and also at a simple iteration over a dictionary's .items(), I'm seeing what looks like a lot of effort to deal with the fact that iterators return a stream of items, rather than a stream of keys and values. But it's probably not worth changing now.) ChrisA