Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17826

Re: Get named module's file location

References (2 earlier) <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <roy-4780C2.11564723122011@news.panix.com> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> <mailman.4038.1324669945.27778.python-list@python.org> <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6>
Date 2011-12-24 08:21 +1100
Subject Re: Get named module's file location
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4040.1324675319.27778.python-list@python.org> (permalink)

Show all headers | View raw


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 <gnarlodious@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 08:01 -0800
  Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 11:18 -0500
    Re: Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 08:51 -0800
      Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 11:56 -0500
        Re: Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 11:40 -0800
          Re: Get named module's file location Chris Angelico <rosuav@gmail.com> - 2011-12-24 06:52 +1100
            Re: Get named module's file location Chris Angelico <rosuav@gmail.com> - 2011-12-24 08:21 +1100
          Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 15:00 -0500
            Re: Get named module's file location Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 22:39 +0000

csiph-web