On 1/25/14 1:37 AM, seasp...@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)


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

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to