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: 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; '"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; '>>>': 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 To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --089e013d19cc223c5d04e89b0442 Content-Type: text/plain; charset=ISO-8859-1 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 "", line 1, in 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 "", line 1, in 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 --089e013d19cc223c5d04e89b0442 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi Friends,

I am new to Generator= s and was learning the same by experimenting. I wrote a small code which is= as below.

>>> def test_= gen(var):
... =A0 =A0 print "The number is", var
... =A0 =A0= if var % 2 =3D=3D 0:
... =A0 =A0 =A0 =A0 yield var
...= =A0 =A0 else:
... =A0 =A0 =A0 =A0 print "Number is odd"= ;
...=A0
>>>=A0

But when i was executing i saw a behavior i could= not understand. When i gave an even number,
1. The generat= or object was created
2. When i gave next the argument was = yielded.=A0
3. In the subsequent next the Stop Iteration was raised.

>>> res =3D test_gen(78)
>>= ;> res.next()
The number is 78
78
>>= > res.next()
Traceback (most recent call last):
=A0 File "<stdin&= gt;", line 1, in <module>
StopIteration

=
But When i ran with an odd number the result of "Numb= er is odd" is printed. But it seems the generator runs again by itself= to Stop Iteration.=A0

>>> res2 =3D test_gen(77)
>= >> res2.next()
The number is 77
Number is odd
Traceback (most recent call last):
=A0 File "<stdi= n>", line 1, in <module>
StopIteration
>>>=A0

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