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


Groups > comp.lang.python > #88474

Re: generator/coroutine terminology

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: generator/coroutine terminology
Date 2015-04-02 23:46 -0700
Organization A noiseless patient Spider
Message-ID <87h9sx90a5.fsf@jester.gateway.sonic.net> (permalink)
References <ff0bc8eb-63e4-40ea-8d31-301625a3d470@googlegroups.com> <mailman.379.1426371862.21433.python-list@python.org> <87y4mznmj8.fsf@elektro.pacujo.net> <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com> <551a9ebe$0$2987$e4fe514c@dreader35.news.xs4all.nl>

Show all headers | View raw


albert@spenarnc.xs4all.nl (Albert van der Horst) writes:
> You should give an example of usage. As a newby I'm not up to
> figuring out the specification from source for
> something built of the mysterious __ internal
> thingies.

In reality because of generator expressions, the yield statement, and
some useful built-in generators in the itertools module, you rarely
have to use those special methods.  

I'd advise reading through the itertools module documentation from
beginning to end, trying to understand what everything does.  Even if
some are not that useful, it will help convey the mode of thinking that
went into these features.

At a somewhat deeper level you might like the SICP book: 

  http://mitpress.mit.edu/sicp/

It's somewhat old now and it's about Scheme rather than Python, but it
builds up the relevant concepts quite nicely.

Regarding the squares example, consider this even simpler generator:

  def count(n):
   while True:
     yield n
     n += 1

so count(1) yields 1, 2, 3, 4 ...

This is a very useful generator but you don't need to write it since
it's included in the itertools module as itertools.count.  Its initial
value defaults to 0.  So the  squares generator can be written:

   def squares():
      return (i*i for i in itertools.count(0))

Now if you want all the squares less than 100 (i.e. 0, 1, 4, 9, ..., 81):

  wanted = itertools.takewhile(lambda x: x<100, squares())

You can print that out as a list:

   print(list(wanted))

The built-in sum function consumes an iterator, so you can add up the
squares less than 100:

  print(sum(itertools.takewhile(lambda x: x<100, squares())))

this prints 205 which is 1+4+9+16+25+36+49+64+81.

These features fit together quite elegantly and code like this flows off
the fingertips naturally once you've used to it.

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


Thread

Re: generator/coroutine terminology albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-03-31 13:18 +0000
  Re: generator/coroutine terminology Dave Angel <davea@davea.name> - 2015-03-31 09:38 -0400
    Re: generator/coroutine terminology albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-03-31 15:03 +0000
      Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-04-01 02:36 +1100
  Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-03 17:02 +1100
    Re: generator/coroutine terminology albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-04-18 17:52 +0000
  Re: generator/coroutine terminology Paul Rubin <no.email@nospam.invalid> - 2015-04-02 23:46 -0700

csiph-web