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


Groups > comp.lang.python > #66391

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'skip:` 10': 0.07; 'subject:using': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nick': 0.16; 'subject:item': 0.16; 'true:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'seems': 0.21; 'feb': 0.22; 'cc:addr:python.org': 0.22; "shouldn't": 0.24; 'looks': 0.24; 'cc:2**0': 0.24; '15,': 0.26; 'references': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'fri,': 0.33; 'actual': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'yield': 0.36; 'next': 0.36; 'pm,': 0.38; 'previous': 0.38; 'subject:[': 0.39; 'release': 0.40; 'around.': 0.60; 'ian': 0.60; "you'll": 0.62; 'forced': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Nj9LZ2zH8k/q+i1KWSQwpJ5NKaGRSxl+Ke+lmvdTvtQ=; b=p6YEqBoEG3pyI9zLCPRVMZYkScad6lykNQY8Y2z5vyD2YG6AtcEkPvwwtvrgLKhK4U 3CTI7/Fjv2MSOK1ijxOsRZSNICt6WI+5R2b6L9Bxq38OmJf/ry3I64V5wU0BZ83yUDjf aNSFGn2e1JbOnIJTtmWF3tO/AqkKfw7UYP/Yzc265r4fdQhiBm81lQ/QvRyQHgGNZIIb qsjtK64dcERlDaBdTYgqqPr0ak1uhKej7eFBfT/tP+eZF978aM38ZTRH/jhfX1aDeFhS mn0EuRLCEXdIC00gYuKYK1GnPF7Vw+6aYq2PJMXNTtkLxY3jTmh+Mi0dKGhtMi1fK73f v8bQ==
MIME-Version 1.0
X-Received by 10.68.171.229 with SMTP id ax5mr13877792pbc.125.1392450085573; Fri, 14 Feb 2014 23:41:25 -0800 (PST)
In-Reply-To <CALwzidk3RTUqSZ1j3tgRQ7rGOHKsjwUwfE7m696Jp9QDvfhfdA@mail.gmail.com>
References <mailman.6952.1392433921.18130.python-list@python.org> <roy-1037EA.22211114022014@news.panix.com> <CAHkxivccycyvqBsBPJMOTixufiTyv3GNCzU4ELgAUv2F+0EExA@mail.gmail.com> <CALwzidk3RTUqSZ1j3tgRQ7rGOHKsjwUwfE7m696Jp9QDvfhfdA@mail.gmail.com>
Date Sat, 15 Feb 2014 18:41:25 +1100
Subject Re: Generator using item[n-1] + item[n] memory
From Chris Angelico <rosuav@gmail.com>
Cc Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.6974.1392450094.18130.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392450094 news.xs4all.nl 2900 [2001:888:2000:d::a6]:38205
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66391

Show key headers only | View raw


On Sat, Feb 15, 2014 at 6:27 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Feb 14, 2014 at 8:31 PM, Nick Timkovich <prometheus235@gmail.com> 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?
>
> Yeah, looks like enumerate also doesn't release its reference to the
> previous object until after it gets the next one.  You'll just have to
> make do without.

You could write your own enumerate function.

def enumerate(it, i=0):
    it = iter(it)
    while True:
        yield i, next(it)
        i += 1

That shouldn't keep any extra references around.

ChrisA

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