day19

List comprehensions produce a complete list. I have done an experiment yesterday. When dealing with a large amount of data, using list comprehension will consume a lot of system resources. Therefore, when dealing with large amounts of data, generator expressions should be used. The generator returns a generator object, not a large list. So this will save a lot of system resources.

Generator function:

def test():
    yield 1
    yield 3
    yield 2
print(test())
result:
<generator object test at 0x00000000011C8150>
其实是返回了一个生成器对象的内存地址。

How to call:

def test():
    yield 1
    yield 3
    yield 2
print(test().__next__())
print:
1

yieldUsed to save the state of the function.
The benefits of generators:
1. Small footprint
2. High efficiency