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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <prometheus235@gmail.com>
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; 'python,': 0.02; 'static': 0.04; 'context': 0.07; 'skip:` 10': 0.07; 'variables': 0.07; 'contexts': 0.09; 'subject:using': 0.09; 'thus,': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'creates': 0.14; 'ah,': 0.16; 'distinct': 0.16; "function's": 0.16; 'nick': 0.16; 'roy': 0.16; 'subject:item': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'seems': 0.21; 'feb': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'closely': 0.24; 'cc:2**0': 0.24; '&gt;': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:& 60': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'prints': 0.31; 'thanks!': 0.32; 'quite': 0.32; 'url:python': 0.33; 'fri,': 0.33; 'actual': 0.34; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'functions.': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'subject:[': 0.39; 'url:mail': 0.40; 'smith': 0.68; 'results': 0.69; 'article': 0.77; 'around,': 0.84; 'forced': 0.84; '\xa0many': 0.84; 'whereas': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=rNpE/rNBKLn1L8GCisGadYBEpxThGDTb0oF89XdER7M=; b=c9IEHjicQ4d9YAgTprtDIP6S3t+l2x3aMdMYs7gbHtgHG3jlOet/7B0Oak5ixeuNj/ xdW8ox11OtFdPBlutqJ4Jthvew/pdb9YHx4VMaYflOjP3fNKBmOD/p6WHyqWUt9vfG67 +GNNeMvuHx3gGZ08ZZUOz7IYTyioWccMn7kwLWntn3vjIEFbSIykPKJxCCdkjlZBVk7G bWtUlivslxZL48I1E6xV4TF6J17c3l40eA+xZufuWaMAv05dvMnlPU9lky16CGEZPXJB R8rJ3Y6l9rBwuLYbYhNShYFwg9aTIyDZc+30u9K1crysG7/BQaCQ+v/SLXajZxGBxwum 7Msg==
X-Received by 10.194.90.144 with SMTP id bw16mr8531633wjb.1.1392435130406; Fri, 14 Feb 2014 19:32:10 -0800 (PST)
MIME-Version 1.0
In-Reply-To <roy-1037EA.22211114022014@news.panix.com>
References <mailman.6952.1392433921.18130.python-list@python.org> <roy-1037EA.22211114022014@news.panix.com>
From Nick Timkovich <prometheus235@gmail.com>
Date Fri, 14 Feb 2014 21:31:49 -0600
Subject Re: Generator using item[n-1] + item[n] memory
To Roy Smith <roy@panix.com>
Content-Type multipart/alternative; boundary=047d7bfcf35c521b0604f2699130
Cc python-list <python-list@python.org>
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.6954.1392435138.18130.python-list@python.org> (permalink)
Lines 99
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392435138 news.xs4all.nl 2835 [2001:888:2000:d::a6]:41918
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66358

Show key headers only | 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