Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56767
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'subsequent': 0.05; 'odd': 0.07; 'generators': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; 'def': 0.12; 'jan': 0.12; 'wrote': 0.14; 'generator.': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'raised.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'res': 0.16; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'friends,': 0.30; 'code': 0.31; '"",': 0.31; 'file': 0.32; '(most': 0.33; 'call.': 0.33; '"the': 0.34; 'could': 0.34; 'created': 0.35; 'but': 0.35; 'there': 0.35; 'executing': 0.36; 'var': 0.36; 'yield': 0.36; 'next': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'number,': 0.60; 'new': 0.61; 'received:173': 0.61; 'first': 0.61; 'happen': 0.63; 'below.': 0.71; 'saw': 0.77; 'behavior': 0.77; 'received:fios.verizon.net': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Doubt on generators behavior |
| Date | Sun, 13 Oct 2013 05:43:50 -0400 |
| References | <CAL0E0u4XEBHihs3F3Q2-OnF=FCos5+CaVO0ZbxZXEfkYzS7d_A@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-59-117-133.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 |
| In-Reply-To | <CAL0E0u4XEBHihs3F3Q2-OnF=FCos5+CaVO0ZbxZXEfkYzS7d_A@mail.gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.1050.1381657446.18130.python-list@python.org> (permalink) |
| Lines | 56 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1381657446 news.xs4all.nl 15930 [2001:888:2000:d::a6]:58843 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:56767 |
Show key headers only | View raw
On 10/13/2013 4:19 AM, Krishnan Shankar wrote: > Hi Friends, > > I am new to Generators and was learning the same by experimenting. I > wrote a small code which is as below. > > >>> def test_gen(var): > ... print "The number is", var > ... if var % 2 == 0: > ... yield var > ... else: > ... print "Number is odd" > ... > >>> > > But when i was executing i saw a behavior i could not understand. When i > gave an even number, > 1. The generator object was created > 2. When i gave next the argument was yielded. > 3. In the subsequent next the Stop Iteration was raised. > > >>> res = test_gen(78) > >>> res.next() > The number is 78 > 78 > >>> res.next() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > StopIteration > > But When i ran with an odd number the result of "Number is odd" is > printed. But it seems the generator runs again by itself to Stop Iteration. When number is odd, there is nothing to yield, so it raises StopIteration on the first next call. > >>> res2 = test_gen(77) > >>> res2.next() > The number is 77 > Number is odd > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > StopIteration > >>> > > How did this happen automatically? I am not able to get the execution of > a generator. Can someone please help me in understanding? > Regards, > Krishnan > > -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Doubt on generators behavior Terry Reedy <tjreedy@udel.edu> - 2013-10-13 05:43 -0400
csiph-web