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


Groups > comp.lang.python > #64742

Re: should I transfer 'iterators' between functions?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; "subject:' ": 0.07; 'test,': 0.07; '[1,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'iterator': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'subject:between': 0.16; 'types,': 0.16; 'zero.': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'received:comcast.net': 0.24; 'refers': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'leave': 0.29; 'am,': 0.29; 'concern': 0.31; 'types.': 0.31; 'values.': 0.31; 'covered': 0.32; 'could': 0.34; 'but': 0.35; 'returning': 0.36; 'subject:?': 0.36; 'detail': 0.37; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'referred': 0.60; 'here:': 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'drops': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: should I transfer 'iterators' between functions?
Date Sat, 25 Jan 2014 07:55:10 -0500
References <6ad4232c-a8d9-4195-9edd-65c0e35923a7@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <6ad4232c-a8d9-4195-9edd-65c0e35923a7@googlegroups.com>
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.5974.1390654524.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390654524 news.xs4all.nl 2852 [2001:888:2000:d::a6]:60151
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64742

Show key headers only | View raw


On 1/25/14 1:37 AM, seaspeak@gmail.com wrote:
> take the following as an example, which could work well.
> But my concern is, will list 'l' be deconstructed after function return? and then iterator point to nowhere?
>
> def test():
>      l = [1, 2, 3, 4, 5, 6, 7, 8]
>      return iter(l)
> def main():
>      for i in test():
>          print(i)
>
>

The two things to understand here are names, and values.  When you leave 
the function test, the name l goes away, because it is a locally scoped 
name.  It referred to a value, your list.  But values have reference 
counts, and are not reclaimed until their reference count drops to zero.

Your function is returning a value, a listiterator, and that 
listiterator refers to the list, so the list won't be reclaimed.

Names have scopes but no types.  Values have types, but no scope.  They 
live as long as they are referred to by something.

This is covered in more detail here: 
http://nedbatchelder.com/text/names.html


-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

should I transfer 'iterators' between functions? seaspeak@gmail.com - 2014-01-24 22:37 -0800
  Re: should I transfer 'iterators' between functions? Chris Angelico <rosuav@gmail.com> - 2014-01-25 17:43 +1100
  Re: should I transfer 'iterators' between functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-25 06:45 +0000
  Re: should I transfer 'iterators' between functions? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-25 07:55 -0500
  Re: should I transfer 'iterators' between functions? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-25 07:56 -0500
  Re: should I transfer 'iterators' between functions? Peter Otten <__peter__@web.de> - 2014-01-25 14:32 +0100

csiph-web