Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:module': 0.04; 'python': 0.08; "(it's": 0.09; 'brackets': 0.09; 'read.': 0.10; 'am,': 0.12; 'skip:[ 20': 0.12; 'subject:file': 0.12; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guessing': 0.16; 'iterates': 0.16; 'parentheses': 0.16; 'subject:named': 0.16; 'meant': 0.17; 'wrote:': 0.18; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'sat,': 0.25; 'module': 0.26; "i'm": 0.26; 'do.': 0.28; 'version,': 0.28; 'message-id:@mail.gmail.com': 0.28; '24,': 0.29; 'idle': 0.29; 'temporary': 0.29; 'modules,': 0.30; 'separately': 0.30; 'chris': 0.30; 'equivalent': 0.31; 'skip:( 20': 0.31; 'does': 0.32; 'list': 0.32; "won't": 0.33; 'actually': 0.33; 'instead': 0.33; 'that,': 0.33; 'to:addr:python- list': 0.34; 'calling': 0.34; 'list.': 0.35; 'two': 0.37; 'but': 0.37; 'list,': 0.37; 'received:google.com': 0.37; 'hoping': 0.38; 'received:209.85': 0.38; 'skip:o 20': 0.38; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'once': 0.60; 'more': 0.61; '2011': 0.61; 'full': 0.62; 'results': 0.63; 'square': 0.67; 'subject:Get': 0.71; 'saving': 0.76; 'each,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=kifyL69O4GI9aEaMDa2ApBWYbLJ5QnZPMZi1+M3gh3s=; b=R7tacz824AA+ETOWeHkQGJT0Kr5d6xg27LcuTQPTsLWr14N/CHUKAcXg4cbjrte30b fINdmid7DUiagzIKwsjTUyPa1Uwh0eyV6Pah6M9g9+qUm9E6wZbG5yRFwB20PD+f86hp HF4E+3PJyJTht8WHIqBOCKGVR+gfqoG4nEgK4= MIME-Version: 1.0 In-Reply-To: <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> Date: Sat, 24 Dec 2011 08:21:56 +1100 Subject: Re: Get named module's file location 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.12 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324675319 news.xs4all.nl 6934 [2001:888:2000:d::a6]:37128 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17826 I'm guessing you meant for this to be on-list, and am hoping you don't mind that I'm replying on-list. On Sat, Dec 24, 2011 at 8:16 AM, Gnarlodious wrote: > Chris Angelico wrote: >> [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for >> module in modules)] >> >> Yeah, a little hard to read. Tell me, does this formulation execute >> imp.find_module(module) once or twice for each modname? What this does is save a temporary list, more or less. (It's actually a generator expression, not a list comprehension, but that's immaterial.) temporary = [imp.find_module(module)[1] for module in modules] [(m, os.path.getmtime(m)) for m in temporary] It iterates over modules, calling find_module for each, and saving the results to a new list. Then separately iterates over the new list, pairing each with the getmtime. Since I used parentheses instead of square brackets in the original expression, Python won't actually build the full list. Other than that, it's equivalent to the two-statement version, and you can try those two in IDLE to see what they do. ChrisA