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


Groups > comp.lang.python > #88383

Re: generator/coroutine terminology

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; '16,': 0.03; 'lines,': 0.07; 'squares': 0.07; 'builtin': 0.09; 'method,': 0.09; 'def': 0.12; 'iterator': 0.16; 'squared': 0.16; 'subject:generator': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'written': 0.21; 'example': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'source': 0.25; 'subject:/': 0.26; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; '(although': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'sequence': 0.36; 'yield': 0.36; "didn't": 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'break': 0.61; 'numbers': 0.61; 'further': 0.61; 'first': 0.61; 'charset:windows-1252': 0.65; 'biggest': 0.67; 'received:74.208': 0.68; 'article': 0.77; 'experiment': 0.84; 'try,': 0.84; 'usage.': 0.84; 'albert': 0.91
Date Tue, 31 Mar 2015 09:38:16 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: generator/coroutine terminology
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>
In-Reply-To <551a9ebe$0$2987$e4fe514c@dreader35.news.xs4all.nl>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V03:K0:PXMQtKliHms4/lRKSB/pnHuqqcgBWO8rr/GCZBc4xT4h53hyVE6 CXq9uwKVLMy4hUzk3LXBWPglZCdl/kaZger+yFnau6OM2VAPzhtYnewQsRAN/V0EbZIdkc8 e73HuGvpVCDxj7D80L9b27IK2o6QKcdqMxcIralr0bDJdsciXJBp7xRJl3QYgkZBwGWshZi i2fIMNr6pW90/nGdXdk2A==
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.372.1427809109.10327.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427809109 news.xs4all.nl 2837 [2001:888:2000:d::a6]:38853
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88383

Show key headers only | View raw


On 03/31/2015 09:18 AM, 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.)
>

He did say it was an iterator.  So for a first try, write a for loop:

class Squares:
    def __init__(self):
        self.i = 0
    def __next__(self):
        self.i += 1
        return self.i**2
    def __iter__(self):
        return self

for i in Squares():
     print(i)
     if i > 50:
         break

print("done")


>
>>
>>
>> Here's the same thing written as a generator:
>>
>> def squares():
>>     i = 1
>>     while True:
>>         yield i**2
>>         i += 1
>>
>>
>> Four lines, versus eight. The iterator version has a lot of boilerplate
>> (although some of it, the two-line __iter__ method, could be eliminated if
>> there was a standard Iterator builtin to inherit from).
>>
-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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