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


Groups > comp.lang.python > #56764

Doubt on generators behavior

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <i.am.songoku@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'subsequent': 0.05; '&quot;the': 0.07; 'odd': 0.07; 'generators': 0.09; 'runs': 0.10; 'def': 0.12; 'wrote': 0.14; 'generator.': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'raised.': 0.16; 'res': 0.16; 'seems': 0.21; '>>>': 0.22; 'print': 0.22; '&gt;&gt;&gt;': 0.24; 'friends,': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; '"",': 0.31; 'skip:7 10': 0.31; 'file': 0.32; '(most': 0.33; '"the': 0.34; 'could': 0.34; 'created': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'var': 0.36; 'yield': 0.36; 'next': 0.36; 'skip:o 20': 0.38; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'number,': 0.60; 'new': 0.61; 'happen': 0.63; 'below.': 0.71; 'saw': 0.77; 'behavior': 0.77
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=rAWjAjZmN6EHsf2siDh7T/EC6zygBdnig1qfvWQYJSQ=; b=kyxib7clDIvvrCbnY/RBuOG9T6RAVplqy3E8x0hurj3+dVXMRcd7F93gW6x1RMGo1L 8rnTBeklS923M82cS5MHTwhcCwhYenGSa2nQpxHTnw6+jw/XIy7saiQgb3fsC4NnQzGB LUbesc3tsfScxkfqIKW5Dy9jIwIKBc/oydjO/jF3PuB81Ii7l/BHILDbDjOPuAl+cpw5 tjdFRtvwRalAl6g47Dqxg3cNMeGXDTckS+tzMv+P/g2xOUyqFIiKef1sVgcjLBx0zJ50 NcWa1NRHBotqX5pkMs12rA0rIQJ5aJ9REHsQr/TY+klcf9WVIF1qZRoQIvgijtSBJ409 CFQw==
MIME-Version 1.0
X-Received by 10.194.240.197 with SMTP id wc5mr25116603wjc.23.1381652393768; Sun, 13 Oct 2013 01:19:53 -0700 (PDT)
Date Sun, 13 Oct 2013 13:49:53 +0530
Subject Doubt on generators behavior
From Krishnan Shankar <i.am.songoku@gmail.com>
To Python <python-list@python.org>
Content-Type multipart/alternative; boundary=089e013d19cc223c5d04e89b0442
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.1049.1381652399.18130.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381652399 news.xs4all.nl 16000 [2001:888:2000:d::a6]:44283
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56764

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

>>> 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

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


Thread

Doubt on generators behavior Krishnan Shankar <i.am.songoku@gmail.com> - 2013-10-13 13:49 +0530
  Re: Doubt on generators behavior Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-13 11:39 +0300
  Re: Doubt on generators behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-13 09:14 +0000

csiph-web