Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87319 > unrolled thread
| Started by | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| First post | 2015-03-12 06:35 -0700 |
| Last post | 2015-03-12 22:22 +0200 |
| Articles | 6 on this page of 86 — 15 participants |
Back to article view | Back to comp.lang.python
generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-12 06:35 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-13 00:55 +1100
Re: generator/coroutine terminology breamoreboy@gmail.com - 2015-03-12 06:57 -0700
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-13 03:27 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-12 09:52 -0700
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-12 19:55 +0200
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-12 19:23 -0700
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-13 14:30 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-12 22:28 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-13 19:23 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-13 02:12 -0700
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-13 11:36 +0200
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-14 17:04 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-14 09:54 +0200
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-14 08:04 +0000
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-14 10:30 +0200
Re: generator/coroutine terminology Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-14 14:14 -0600
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-14 21:15 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-14 20:31 +0000
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-14 08:29 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 02:56 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-14 08:59 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 03:14 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-14 09:33 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 03:51 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-14 10:17 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-14 16:56 +0000
Re: generator/coroutine terminology Dave Angel <davea@davea.name> - 2015-03-14 13:07 -0400
Re: generator/coroutine terminology albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-03-31 12:57 +0000
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-15 19:37 +1100
Re: generator/coroutine terminology CHIN Dihedral <dihedral88888@gmail.com> - 2015-04-18 11:07 -0700
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-13 22:32 +1100
Re: generator/coroutine terminology Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-03-14 22:02 +0000
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-15 00:15 +0200
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 09:24 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-15 02:15 +0200
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 11:22 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-15 02:48 +0200
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-15 13:02 +1100
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-16 12:03 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 09:12 +0200
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-16 18:21 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 09:40 +0200
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-16 22:59 +1100
Re: generator/coroutine terminology Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 01:37 -0600
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 09:52 +0200
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-16 23:02 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 14:42 +0200
Re: generator/coroutine terminology Jonas Wielicki <jonas@wielicki.name> - 2015-03-16 13:39 +0100
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-16 19:36 +1100
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-16 19:58 +1100
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-16 22:51 +1100
Re: generator/coroutine terminology Chris Angelico <rosuav@gmail.com> - 2015-03-17 00:16 +1100
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 14:32 +0200
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 05:51 -0700
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 15:13 +0200
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-17 01:32 +1100
Re: generator/coroutine terminology Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 08:45 -0600
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-17 00:39 +1100
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 07:19 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-16 14:26 +0000
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 07:37 -0700
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 07:55 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-16 18:19 +0000
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 19:52 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-17 03:07 +0000
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 20:18 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-17 03:25 +0000
Re: generator/coroutine terminology Rustom Mody <rustompmody@gmail.com> - 2015-03-16 20:33 -0700
Re: generator/coroutine terminology Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-17 03:55 +0000
Re: generator/coroutine terminology Mario Figueiredo <marfig@gmail.com> - 2015-03-17 04:22 +0100
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-17 01:35 +1100
Re: generator/coroutine terminology Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-17 01:36 +1100
Re: generator/coroutine terminology Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 08:52 -0600
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 17:09 +0200
Re: generator/coroutine terminology Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 09:26 -0600
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-16 18:05 +0200
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
Re: generator/coroutine terminology Terry Reedy <tjreedy@udel.edu> - 2015-03-12 16:11 -0400
Re: generator/coroutine terminology Marko Rauhamaa <marko@pacujo.net> - 2015-03-12 22:22 +0200
Page 5 of 5 — ← Prev page 1 2 3 4 [5]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-01 02:36 +1100 |
| Message-ID | <mailman.378.1427816214.10327.python-list@python.org> |
| In reply to | #88387 |
On Wed, Apr 1, 2015 at 2:03 AM, Albert van der Horst <albert@spenarnc.xs4all.nl> wrote: > class Squares: > def __init__(self): > self.i = 0 > def __next__(self): > self.i += 1 > return self.i**2 > def __iter__(self): > return self > > albert@cherry:/tmp$ python > Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> from aap import * >>>> for i in Squares(): > ... print i > ... if i>50: break > ... > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: instance has no next() method >>>> > > / -------------------------- > > Probably not what is intended. > > Last minute note: > renaming __next__() into next() did the job. That class was written for Python 3, not Python 2. In Py2, you need to rename __next__ to next, as you noted, and you probably also want to explicitly subclass object. Or just run it under Python 3. :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-04-03 17:02 +1100 |
| Message-ID | <551e2cfd$0$11123$c3e8da3@news.astraweb.com> |
| In reply to | #88380 |
On Wednesday 01 April 2015 00:18, Albert van der Horst wrote:
> In article <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com>,
> Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
>>The biggest difference is syntactic. Here's an iterator which returns a
>>never-ending sequence of squared numbers 1, 4, 9, 16, ...
>>
>>class Squares:
>> def __init__(self):
>> self.i = 0
>> def __next__(self):
>> self.i += 1
>> return self.i**2
>> def __iter__(self):
>> return self
>
> 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.
> (I did experiment with Squares interactively. But I didn't get
> further than creating a Squares object.)
Ah, sorry about that!
Usage is:
it = Squares() # create an iterator
print(next(it)) # print the first value
x = next(it) # extract the second
while x < 100:
print(x)
x = next(it)
Beware of doing this:
for x in Squares():
print(x)
since Squares is an *infinite* generator, it will continue for ever if you
let it. Fortunately you can hit Ctrl-C to interrupt the for loop at any
point.
In Python 2, you will need to rename __next__ to just next without the
double-leading-and-trailing underscores.
>>Here's the same thing written as a generator:
>>
>>def squares():
>> i = 1
>> while True:
>> yield i**2
>> i += 1
And for this one:
it = squares() # create the iterator
print(next(it)) # print the first value
x = next(it) # extract the second
while x < 100:
print(x)
x = next(it)
Usage is pretty much exactly the same.
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | albert@spenarnc.xs4all.nl (Albert van der Horst) |
|---|---|
| Date | 2015-04-18 17:52 +0000 |
| Message-ID | <553299ef$0$21225$e4fe514c@dreader37.news.xs4all.nl> |
| In reply to | #88472 |
In article <551e2cfd$0$11123$c3e8da3@news.astraweb.com>, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: >On Wednesday 01 April 2015 00:18, Albert van der Horst wrote: > >> In article <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com>, >> Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > >>>The biggest difference is syntactic. Here's an iterator which returns a >>>never-ending sequence of squared numbers 1, 4, 9, 16, ... >>> >>>class Squares: >>> def __init__(self): >>> self.i = 0 >>> def __next__(self): >>> self.i += 1 >>> return self.i**2 >>> def __iter__(self): >>> return self >> >> 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. >> (I did experiment with Squares interactively. But I didn't get >> further than creating a Squares object.) > > >Ah, sorry about that! > >Usage is: > >it = Squares() # create an iterator >print(next(it)) # print the first value >x = next(it) # extract the second >while x < 100: > print(x) > x = next(it) > > >Beware of doing this: > >for x in Squares(): > print(x) > >since Squares is an *infinite* generator, it will continue for ever if you >let it. Fortunately you can hit Ctrl-C to interrupt the for loop at any >point. > >In Python 2, you will need to rename __next__ to just next without the >double-leading-and-trailing underscores. > > >>>Here's the same thing written as a generator: >>> >>>def squares(): >>> i = 1 >>> while True: >>> yield i**2 >>> i += 1 > >And for this one: > >it = squares() # create the iterator >print(next(it)) # print the first value >x = next(it) # extract the second >while x < 100: > print(x) > x = next(it) > > >Usage is pretty much exactly the same. Thanks, I get it now. next and yield are more or less switching between coroutines. > > >-- >Steve > -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2015-04-02 23:46 -0700 |
| Message-ID | <87h9sx90a5.fsf@jester.gateway.sonic.net> |
| In reply to | #88380 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-03-12 16:11 -0400 |
| Message-ID | <mailman.299.1426191095.21433.python-list@python.org> |
| In reply to | #87319 |
On 3/12/2015 9:35 AM, Rustom Mody wrote: > This is more a question about standard terminology/conventions than about semantics - of course assuming I understand :-) > > Say I have a simple yielding function: > > def foo(x): > yield x+1 > yield x+2 This is a generator function > And I have > > g = foo(2) > > If I look at type, g's type is 'generator' whereas foo is just plain-ol 'function.' > > Whereas in informal usage we say foo is a generator. I do not, because it does cause confusion, in spite of denials by people who use 'generator' ambiguously. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2015-03-12 22:22 +0200 |
| Message-ID | <87pp8eq82u.fsf@elektro.pacujo.net> |
| In reply to | #87329 |
Terry Reedy <tjreedy@udel.edu>: > On 3/12/2015 9:35 AM, Rustom Mody wrote: >> This is more a question about standard terminology/conventions than >> about semantics - of course assuming I understand :-) >> >> Say I have a simple yielding function: >> >> def foo(x): >> yield x+1 >> yield x+2 > > This is a generator function Which in the official language specification is an elaborate synonym of a generator. Marko
[toc] | [prev] | [standalone]
Page 5 of 5 — ← Prev page 1 2 3 4 [5]
Back to top | Article view | comp.lang.python
csiph-web