Understanding generators in Python

前端 未结 12 1547
日久生厌
日久生厌 2020-11-21 05:46

I am reading the Python cookbook at the moment and am currently looking at generators. I\'m finding it hard to get my head round.

As I come from a Java background, i

12条回答
  •  我在风中等你
    2020-11-21 06:12

    A generator is effectively a function that returns (data) before it is finished, but it pauses at that point, and you can resume the function at that point.

    >>> def myGenerator():
    ...     yield 'These'
    ...     yield 'words'
    ...     yield 'come'
    ...     yield 'one'
    ...     yield 'at'
    ...     yield 'a'
    ...     yield 'time'
    
    >>> myGeneratorInstance = myGenerator()
    >>> next(myGeneratorInstance)
    These
    >>> next(myGeneratorInstance)
    words
    

    and so on. The (or one) benefit of generators is that because they deal with data one piece at a time, you can deal with large amounts of data; with lists, excessive memory requirements could become a problem. Generators, just like lists, are iterable, so they can be used in the same ways:

    >>> for word in myGeneratorInstance:
    ...     print word
    These
    words
    come
    one
    at 
    a 
    time
    

    Note that generators provide another way to deal with infinity, for example

    >>> from time import gmtime, strftime
    >>> def myGen():
    ...     while True:
    ...         yield strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())    
    >>> myGeneratorInstance = myGen()
    >>> next(myGeneratorInstance)
    Thu, 28 Jun 2001 14:17:15 +0000
    >>> next(myGeneratorInstance)
    Thu, 28 Jun 2001 14:18:02 +0000   
    

    The generator encapsulates an infinite loop, but this isn't a problem because you only get each answer every time you ask for it.

提交回复
热议问题