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


Groups > comp.lang.python > #82768

Re: List Comprehensions

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: List Comprehensions
Date 2014-12-22 08:07 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-8A482E.08070922122014@news.panix.com> (permalink)
References <CACT3xuWVZPqEZ-hQ6QS7QTnpuBG3FXvVuc=wVN5PLLM7FPrscQ@mail.gmail.com> <mailman.17109.1419228400.18130.python-list@python.org>

Show all headers | View raw


In article <mailman.17109.1419228400.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Mon, Dec 22, 2014 at 4:42 PM, Ganesh Pal <ganesh1pal@gmail.com> wrote:
> > (a)  I was trying to reduce the below piece of code using List comprehension
> > ? Any suggestion please let me know
> >
> >
> >      for opt in options:
> >           opt['result'] = Queue.Queue()
> >           tmp_thread = pause.Thread(opt)
> >           threads.append(tmp_thread)
> >            tmp_thread.start()
> >
> > (b)    Is there anything that I need to consider while using list
> > comprehension with threads ?
> 
> Your code is doing several things at once, so it's probably not worth
> trying to turn it into a comprehension. I don't think it needs to be
> shortened, anyway; looks fine to me.

I pretty much agree with Chris.  If I were to refactor this, however, I 
would probably pull the body of the loop out into a function, then it 
might make sense to do the accumulation of threads in a comprehension:

def init_thread(opt):
   opt['result'] = Queue.Queue()
   thread = pause.Thread(opt)
   thread.start()
   return thread

threads = [init_thread(opt) for opt in options]

But, really, I think the way you had it originally was cleaner.

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


Thread

Re: List Comprehensions Chris Angelico <rosuav@gmail.com> - 2014-12-22 17:06 +1100
  Re: List Comprehensions Roy Smith <roy@panix.com> - 2014-12-22 08:07 -0500
    Re: List Comprehensions Chris Angelico <rosuav@gmail.com> - 2014-12-23 00:21 +1100
      Re: List Comprehensions Rustom Mody <rustompmody@gmail.com> - 2014-12-22 06:58 -0800
        Re: List Comprehensions Chris Angelico <rosuav@gmail.com> - 2014-12-23 02:07 +1100
          Re: List Comprehensions Rustom Mody <rustompmody@gmail.com> - 2014-12-22 09:45 -0800

csiph-web