current position:Home>How IOS developers learn Python Programming 13 - function 4

How IOS developers learn Python Programming 13 - function 4

2022-01-31 20:03:34 Sissy's father

This is my participation 11 Yue Gengwen challenges 13 God , Check out the activity details :2021 The last time Gengwen challenge .

generator (generator

All data in the list is in memory , If there is a large amount of data, it will consume a lot of memory .

for example , We just need to access the first few elements , But the memory occupied by most of the latter elements will be wasted .
Then the generator is to continuously calculate the subsequent elements according to the algorithm in the process of loop , So you don't have to create an entire list , To save a lot of space .
To make a long story short , When we want to use huge data , And want it to take up less space , Then use the generator .

How to create a generator

Generator Expressions

Generator Expressions From a combination of iteration and list parsing , Generators are similar to list parsing , But it uses () instead of []

g = (x for x in range(5))
print(g)       # generator object
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
#  Beyond error reporting 
print(next(g))

for i in g:
    print(i)
 Copy code 

generator

When a function contains yield keyword , Then this function is no longer an ordinary function , It is a generator. generator Expressions come from a combination of iteration and list parsing , generator Similar to list parsing , But it uses () instead of []. Calling a function creates a generator object . It works by repeatedly calling next() perhaps __next__() Method , Until an exception is caught :

def yieldtest(number):
    n = 0
    # li = []
    while n<number:
        # li.append(n)
        yield n
        n+=1

res = yieldtest(20)
print(res)       # generator object
print(next(res)) # 0
print(next(res)) # 1
print(next(res)) # 2
 Copy code 

️ Be careful :

  • yield Returns a value , And remember the location of this return value , Next time I meet next() Invocation time , Code from yield The next statement of begins execution . And return The difference is ,return It also returns a value , But end the function directly .

for example , Realization of Fibonacci series . Except for the first and second numbers , Any number can be obtained by adding the first two :1,1,2,3,5,8,12,21,34.....

def createNums():
    print("-----func start-----")
    a,b = 0,1
    for i in range(5):
        # print(b)
        print("--1--")
        yield b
        print("--2--")
        a,b = b,a+b
        print("--3--")
    print("-----func end-----")
    
g = createNums()
print(next(g))  
print(next(g))  
print(next(g))
print(next(g))
print(next(g))
 Copy code 

send()

send()  and next() equally , Can make the generator go one step further ( encounter yield return ), but send() Can pass a value , This value serves as yield The result of the expression as a whole :

def test():
    a1 = yield "hello"
    print("---1---")
    yield a1

res = test()
print(next(res))          # "hello"
print(res.send("world"))  # "world"
 Copy code 

in other words , adopt send Method can forcibly modify the previous yield Expression value .

For example, there is a function yield assignment :

a1 = yield "hello"
 Copy code 
  1. The first iteration here will return "hello", however a1 Not assigned yet ;
  2. In the second iteration , Use .send("world");
  3. that , It is equivalent to forcibly modifying yield "hello" The value of the expression is "world";
  4. therefore yield a1 The result is "world".

copyright notice
author[Sissy's father],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201312003312991.html

Random recommended