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


Groups > comp.lang.python > #66428

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'from:addr:yahoo.co.uk': 0.04; 'static': 0.04; 'context': 0.07; 'skip:` 10': 0.07; 'variables': 0.07; 'contexts': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'thus,': 0.09; 'def': 0.12; 'creates': 0.14; 'language.': 0.14; 'ah,': 0.16; 'distinct': 0.16; "function's": 0.16; 'nick': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'subject:item': 0.16; 'language': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'thanks.': 0.20; 'seems': 0.21; 'feb': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'closely': 0.24; '---': 0.24; 'post': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '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; '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; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'subject:[': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'free': 0.61; 'viruses': 0.61; 'protection': 0.63; 'our': 0.64; 'antivirus': 0.68; 'smith': 0.68; 'results': 0.69; 'article': 0.77; 'around,': 0.84; 'forced': 0.84; 'whereas': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Generator using item[n-1] + item[n] memory
Date Sat, 15 Feb 2014 12:28:53 +0000
References <mailman.6952.1392433921.18130.python-list@python.org> <roy-1037EA.22211114022014@news.panix.com> <CAHkxivccycyvqBsBPJMOTixufiTyv3GNCzU4ELgAUv2F+0EExA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-146-14-108.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <CAHkxivccycyvqBsBPJMOTixufiTyv3GNCzU4ELgAUv2F+0EExA@mail.gmail.com>
X-Antivirus avast! (VPS 140215-0, 15/02/2014), Outbound message
X-Antivirus-Status Clean
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.6990.1392467409.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392467409 news.xs4all.nl 2859 [2001:888:2000:d::a6]:37807
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66428

Show key headers only | View raw


On 15/02/2014 03:31, Nick Timkovich wrote:
> 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
> <mailto:roy@panix.com>> wrote:
>
>     In article <mailman.6952.1392433921.18130.python-list@python.org
>     <mailto:mailman.6952.1392433921.18130.python-list@python.org>>,
>       Nick Timkovich <prometheus235@gmail.com
>     <mailto: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
>

Nick, please don't top post on this list, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Back to comp.lang.python | Previous | NextPrevious 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