Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88391
| References | <ff0bc8eb-63e4-40ea-8d31-301625a3d470@googlegroups.com> <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com> <551a9ebe$0$2987$e4fe514c@dreader35.news.xs4all.nl> <mailman.372.1427809109.10327.python-list@python.org> <551ab739$0$3042$e4fe514c@dreader35.news.xs4all.nl> |
|---|---|
| Date | 2015-04-01 02:36 +1100 |
| Subject | Re: generator/coroutine terminology |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.378.1427816214.10327.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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