# Use of generator functions (67)

1. What is a generator

• Generator is a subclass of iterator, which can be regarded as a special iterator. Generator supports all operations and features of iterator

2. Generator Expressions

• a. Basic syntax: tuple = (i for i in xxx)

```set = (i for i in range(10)) print(set)```

• b. Deductive combined conditional statement syntax: tuple = (i for i in xxx if condition)

• c. Deduction combined with ternary operator syntax: tuple = (value if condition is true if condition else condition is not true value for i in xxx)

3. What is a generator function

• Functions that use the keyword yield are called generator functions
• When the generator function is called, it will not be executed directly, and a generator object will be returned directly (hint:

Example 1: This is a generator function:

```def work():
print("-------------------------- 1 --------------------------")
yield 1111
print("-------------------------- 2 --------------------------")
yield 2222
print("-------------------------- 3 --------------------------")
yield 3333
print("-------------------------- 4 --------------------------")
yield 4444

res = work()
print(res)```

• When making a function call, no data is returned, even if [yield] returns data 2222

• The print returns the generator object:

• The generator function must use the generator to generate data: next() function, the data will come out, and the unique data will be taken out each time

```</p> <h1>Use the generator to generate data: next() function</h1> <p>print ( "The data generated for the first time is: " , next (res)) print ( "The data generated for the second time is: " , next (res)) print ( " The data generated for the third time is: " , next (res)) print ( "The data generated for the fourth time is: " , next (res)) ```

You can see the currently returned data:

• There are currently four sets of data in this function, all of which have been taken out using the next() function. There is no data when it is generated for the fifth time, and an error will be reported. The iterator has stopped working: StopIteration

4. Three methods inside the generator

• The generator provides three methods: close method, send method, throw method

1. close method: manually close the generator

• Function: After the generator is closed, subsequent operations will be performed, and an error will be reported. The iterator has stopped working: StopIteration

Example 1:

```def work():
print("-------------------------- 1 --------------------------")
yield 1111
print("-------------------------- 2 --------------------------")
yield 2222
print("-------------------------- 3 --------------------------")
yield 3333
print("-------------------------- 4 --------------------------")
yield 4444

# create a generator
res = work()

# Use the generator to generate data: next() function
print( "The data generated for the first time is: " , next(res))

# Use the close method: close the generator manually, without executing the following steps
res.close()

# After closing the generator and performing subsequent operations, an error will be reported that the iterator has stopped working: StopIteration
print( "The data generated for the second time is:" ,next(res))```

Results of the:

• It can be seen that after the first data is generated, the close method is used to close the generator, and the error iterator has stopped working: StopIteration

2. send method: generate data

• Like the built-in function next, it is used to generate data
• But while generating data, you can interact with data inside the generator

important point:

• Before using the send method, you must first generate data at least once through the next method (next to start the generator), otherwise an error will be reported
• To use the send method, you must pass a parameter (value)
• When passed in using the send() method, it will be passed to the inside of the generator last time, where the yield is suspended, received with a value, and printed out

Example 2:

```def work():
print("-------------------------- 1 --------------------------")
s1= yield 1111
print("-------------------------- 2 --------------------------",s1)
s2= yield 2222
print("-------------------------- 3 --------------------------",s2)
s3= yield 3333
print("-------------------------- 4 --------------------------",s3)
yield 4444

# create a generator
res = work()

print(res.send("hen1"))
print(res.send("hen2"))
print(res.send("hen3"))```

The execution result will report an error, why?

Before using the send method, the data must be generated at least once through the next method.

```# create a generator
res = work()
print ( next (res))   # Generate a piece of data through the next method

# Using the send method to pass values
​​print (res.send( "hen1" ))
print (res.send( "hen2" ))
print (res.send( "hen3" ))```

Results of the:

• When passed in using the send() method, it will be passed inside the generator last time, where the yield is suspended

3. Throw method: Actively throw the specified exception inside the generator

• Equivalent to raising ValueError inside the generator, the exception is thrown

Example 3:

```def work():
print("-------------------------- 1 --------------------------")
s1= yield 1111
print("-------------------------- 2 --------------------------",s1)
s2= yield 2222
print("-------------------------- 3 --------------------------",s2)
s3= yield 3333
print("-------------------------- 4 --------------------------",s3)
yield 4444

# create a generator
res = work()

# Using the throw method, throw an exception
res.throw(ValueError)    # Equivalent to raising ValueError inside the generator```

Execution result:
insert picture description here