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


Groups > comp.lang.python > #66358

Re: Generator using item[n-1] + item[n] memory

References <mailman.6952.1392433921.18130.python-list@python.org> <roy-1037EA.22211114022014@news.panix.com>
From Nick Timkovich <prometheus235@gmail.com>
Date 2014-02-14 21:31 -0600
Subject Re: Generator using item[n-1] + item[n] memory
Newsgroups comp.lang.python
Message-ID <mailman.6954.1392435138.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

OK, now the trick; adding `data = None` inside the generator works, but in
my actual code I wrap my generator inside of `enumerate()`, which seems to
obviate the "fix".  Can I get it to play nice or am I forced to count
manually. Is that a feature?


On Fri, Feb 14, 2014 at 9:21 PM, Roy Smith <roy@panix.com> wrote:

> In article <mailman.6952.1392433921.18130.python-list@python.org>,
>  Nick Timkovich <prometheus235@gmail.com> wrote:
>
> > Ah, I think I was equating `yield` too closely with `return` in my head.
> >  Whereas `return` results in the destruction of the function's locals,
> > `yield` I should have known keeps them around, a la C's `static`
> functions.
> >  Many thanks!
>
> It's not quite like C's static.  With C's static, the static variables
> are per-function.  In Python, yield creates a context per invocation.
> Thus, I can do
>
> def f():
>     for i in range(10000):
>         yield i
>
> g1 = f()
> g2 = f()
> print g1.next()
> print g1.next()
> print g1.next()
> print g2.next()
> print g1.next()
>
>
> which prints 0, 1, 2, 0, 3.  There's two contexts active at the same
> time, with a distinct instance of "i" in each one.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: Generator using item[n-1] + item[n] memory Roy Smith <roy@panix.com> - 2014-02-14 22:21 -0500
  Re: Generator using item[n-1] + item[n] memory Nick Timkovich <prometheus235@gmail.com> - 2014-02-14 21:31 -0600
  Re: Generator using item[n-1] + item[n] memory Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-15 00:27 -0700
  Re: Generator using item[n-1] + item[n] memory Chris Angelico <rosuav@gmail.com> - 2014-02-15 18:41 +1100
  Re: Generator using item[n-1] + item[n] memory Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-15 11:03 +0000
  Re: Generator using item[n-1] + item[n] memory Peter Otten <__peter__@web.de> - 2014-02-15 12:27 +0100
  Re: Generator using item[n-1] + item[n] memory Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-15 12:28 +0000

csiph-web