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


Groups > comp.lang.python > #82771

Re: List Comprehensions

References <CACT3xuWVZPqEZ-hQ6QS7QTnpuBG3FXvVuc=wVN5PLLM7FPrscQ@mail.gmail.com> <mailman.17109.1419228400.18130.python-list@python.org> <roy-8A482E.08070922122014@news.panix.com>
Date 2014-12-23 00:21 +1100
Subject Re: List Comprehensions
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.17118.1419254490.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Dec 23, 2014 at 12:07 AM, Roy Smith <roy@panix.com> wrote:
> def init_thread(opt):
>    opt['result'] = Queue.Queue()
>    thread = pause.Thread(opt)
>    thread.start()
>    return thread
>
> threads = [init_thread(opt) for opt in options]

If this is, indeed, just initializing the threads, then this might
make sense. Or alternatively, if you could subclass threading.Thread
and do all the work in __init__, then you could simply construct them
all:

class whatever(thread.Thread):
    def __init__(self, opt):
        self.queue = Queue.Queue()
        self.opt = opt
        super().__init__()
        self.start()

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

Just as long as you can come up with a sane name for the class, or the
initializer function, that makes sense without the list comp.

Incidentally, this is part of what I was saying about side effects
being okay in a list comp; either Roy's or my examples here would be a
list comp that has the side effect of starting a bunch of threads, and
I don't see it as being at all problematic. Just don't use a list comp
for _just_ the side effects.

ChrisA

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