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


Groups > comp.lang.python > #75287

Re: complete brain fart, it doesn't loop

References <CAHXoDSB=W3S4gwc7aJO2AA6gtkz5VGQ5=y7hZ5tLL32AamjmXQ@mail.gmail.com> <CAPTjJmrdqBwuiuJHSAGCjRZoWsQzi_iFVJe=XD3m0nbGa9w_Cg@mail.gmail.com> <lr3f5l$e4h$1@ger.gmane.org>
Subject Re: complete brain fart, it doesn't loop
From Martin S <shieldfire@gmail.com>
Date 2014-07-27 21:17 +0200
Newsgroups comp.lang.python
Message-ID <mailman.12369.1406488664.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Ah, thanks. Explains it all :) 

/martin 

On 27 Jul 2014, Peter Otten <__peter__@web.de> wrote:
>Chris Angelico wrote:
>
>> On Mon, Jul 28, 2014 at 3:53 AM, Martin S <shieldfire@gmail.com>
>wrote:
>>> I have this snippet in my web application. Question is why doesn't
>the
>>> stupid thing loop ten times? It loops exactly 1 time.
>>>
>>>   # Reset counter
>>>     counter = 0
>>>
>>>     while counter <= 10:
>>>
>>>         return "<p>Long line with games</p>"
>>>
>>>         counter=counter+1
>> 
>> When you hit the 'return', it stops the function immediately :)
>
>By the way, Python has something similar to a function that temporarily
>
>interrupts execution but preserves state. It's called generator.
>Compare:
>
>>>> def f():
>...     for i in range(3):
>...             return "<p>Long line with games</p>"
>... 
>>>> f()
>'<p>Long line with games</p>'
>>>> def g():
>...     for i in range(3):
>...             yield "<p>Long line with games</p>"
>... 
>>>> g()
><generator object g at 0x7fe9b3e7b690>
>>>> list(g())
>['<p>Long line with games</p>', '<p>Long line with games</p>', '<p>Long
>line 
>with games</p>']

-- Sent with K-@ Mail - the evolution of emailing.

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


Thread

Re: complete brain fart, it doesn't loop Martin S <shieldfire@gmail.com> - 2014-07-27 21:17 +0200

csiph-web