Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66415
| Path | csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'importerror:': 0.07; 'skip:` 10': 0.07; 'though:': 0.07; '"__main__":': 0.09; '__name__': 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; 'try:': 0.09; 'yeah,': 0.09; 'def': 0.12; 'itertools': 0.16; 'len(self)': 0.16; 'nick': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:item': 0.16; 'true:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'seems': 0.21; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; 'looks': 0.24; '15,': 0.26; 'references': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'thus': 0.29; "doesn't": 0.30; 'ist': 0.31; 'code': 0.31; 'class': 0.32; 'fri,': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'except': 0.35; 'but': 0.35; '14,': 0.36; 'yield': 0.36; 'next': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'previous': 0.38; 'subject:[': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'release': 0.40; 'around.': 0.60; 'ian': 0.60; 'new': 0.61; "you'll": 0.62; 'map': 0.64; '20,': 0.68; '10:': 0.84; 'forced': 0.84; 'sizes:': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Generator using item[n-1] + item[n] memory |
| Date | Sat, 15 Feb 2014 12:27:19 +0100 |
| Organization | None |
| References | <mailman.6952.1392433921.18130.python-list@python.org> <roy-1037EA.22211114022014@news.panix.com> <CAHkxivccycyvqBsBPJMOTixufiTyv3GNCzU4ELgAUv2F+0EExA@mail.gmail.com> <CALwzidk3RTUqSZ1j3tgRQ7rGOHKsjwUwfE7m696Jp9QDvfhfdA@mail.gmail.com> <CAPTjJmoUA5KC1mOH_NjYQJase2sAyRe9nRF1zT8DbH7pZ=os+g@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bdacce.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| 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.6983.1392463648.18130.python-list@python.org> (permalink) |
| Lines | 74 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1392463648 news.xs4all.nl 2893 [2001:888:2000:d::a6]:57184 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:66415 |
Show key headers only | View raw
Chris Angelico wrote:
> 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.
An alternative approach ist to yield weak refs and thus have the generator
control the object lifetime. This doesn't work with the built-in list type
though:
import weakref
try:
from itertools import imap # py2
except ImportError:
imap = map # py3
N = 0
def log_deleted(*args):
global N
N -= 1
print("deleted, new N: {}".format(N))
def log_created():
global N
N += 1
print("created, new N: {}".format(N))
def weakrefs(f):
def weakrefs(*args, **kw):
return imap(lambda x: weakref.proxy(x, log_deleted), f(*args, **kw))
return weakrefs
class List(list):
def __str__(self):
s = str(self[:5])
if len(self) > 10:
s = s[:-1] + ", ... ]"
return s
@weakrefs
def biggen():
sizes = 1, 1, 10, 1, 1, 10, 10, 1, 1, 10, 10, 20, 1, 1, 20, 20, 1, 1
for size in sizes:
data = List([1] * int(size * 1e4))
log_created()
yield data
data = None
if __name__ == "__main__":
for i, x in enumerate(biggen()):
print("{} {}".format(i, x))
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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