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: 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>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'closely': 0.24; 'cc:2**0': 0.24; '>': 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: References: From: Nick Timkovich Date: Fri, 14 Feb 2014 21:31:49 -0600 Subject: Re: Generator using item[n-1] + item[n] memory To: Roy Smith Content-Type: multipart/alternative; boundary=047d7bfcf35c521b0604f2699130 Cc: python-list 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: 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 --047d7bfcf35c521b0604f2699130 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > In article , > Nick Timkovich 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 > --047d7bfcf35c521b0604f2699130 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
OK, now the trick; adding `data =3D None` inside the gener= ator works, but in my actual code I wrap my generator inside of `enumerate(= )`, which seems to obviate the "fix". =A0Can I get it to play nic= e or am I forced to count manually. Is that a feature?


On Fri, Feb 1= 4, 2014 at 9:21 PM, Roy Smith <roy@panix.com> wrote:
In article <mailman.6952.1392433921.18130.python-list@python.org>,
=A0Nick Timkovich <prometheus235@gmail.com> wrote:

> Ah, I think I was equating `yield` too closely with `return` in my hea= d.
> =A0Whereas `return` results in the destruction of the function's l= ocals,
> `yield` I should have known keeps them around, a la C's `static` f= unctions.
> =A0Many thanks!

It's not quite like C's static. =A0With C's static,= the static variables
are per-function. =A0In Python, yield creates a context per invocation.
Thus, I can do

def f():
=A0 =A0 for i in range(10000):
=A0 =A0 =A0 =A0 yield i

g1 =3D f()
g2 =3D f()
print g1.next()
print g1.next()
print g1.next()
print g2.next()
print g1.next()


which prints 0, 1, 2, 0, 3. =A0There's two contexts active at the same<= br> time, with a distinct instance of "i" in each one.
--
https://mail.python.org/mailman/listinfo/python-list

--047d7bfcf35c521b0604f2699130--