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


Groups > comp.lang.python > #64744

Re: should I transfer 'iterators' between functions?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; 'explicitly': 0.05; "subject:' ": 0.07; '3),': 0.09; '5)]': 0.09; '[1,': 0.09; 'main()': 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; '(1,': 0.16; '(2,': 0.16; '(3,': 0.16; '0),': 0.16; '1),': 0.16; '2),': 0.16; '4),': 0.16; 'item:': 0.16; 'iterable': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'main():': 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:between': 0.16; 'wrote:': 0.18; 'starts': 0.20; '>>>': 0.22; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'code': 0.31; 'concern': 0.31; 'yes.': 0.31; 'beginning': 0.33; 'could': 0.34; 'common': 0.35; 'but': 0.35; 'returning': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'list': 0.37; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'remove': 0.60; 'email addr:gmail.com': 0.63; 'more': 0.64; 'batchelder': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: should I transfer 'iterators' between functions?
Date Sat, 25 Jan 2014 14:32:18 +0100
Organization None
References <6ad4232c-a8d9-4195-9edd-65c0e35923a7@googlegroups.com> <lc0c9l$qn5$2@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084bd22.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.5976.1390656750.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390656750 news.xs4all.nl 2838 [2001:888:2000:d::a6]:42380
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64744

Show key headers only | View raw


Ned Batchelder wrote:

> 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)
>>
>>
> 
> One more thing: there's no need to call iter() explicitly here.  Much
> more common than returning an iterator from a function is to return an
> iterable.  Your code will work exactly the same if you just remove the
> iter() call:
> 
>      def test():
>          l = [1, 2, 3, 4, 5, 6, 7, 8]
>          return l
>      def main():
>          for i in test():
>              print(i)
 
For that specific client code, yes. In the general case the difference 
matters. Iteration over an iterable starts at the beginning while iteration 
over an iterator starts at the current item:

>>> def main():
...     items = test()
...     print(list(zip(items, items)))
... 
>>> def test(): return range(6) # an iterable
... 
>>> main()
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
>>> def test(): return iter(range(6)) # an iterator
... 
>>> main()
[(0, 1), (2, 3), (4, 5)]
 

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