should I transfer 'iterators' between functions?

Chris Angelico rosuav at gmail.com
Sat Jan 25 01:43:58 EST 2014


On Sat, Jan 25, 2014 at 5:37 PM,  <seaspeak at 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)

Perfectly safe. Python guarantees that nothing can ever point to
"nowhere"; everything that might be looking for something else will
hold a reference to it, so the thing referred to will hang around.

ChrisA



More information about the Python-list mailing list