What is a generator?
The yield function is called a generator. A generator is a function that returns an iterator. Generators are iterators and can only be used for iterative operations. Test interview guide
Don’t understand? It doesn’t matter, here is a Fibonacci number column example to explain:
A Fibonacci Sequence generated by a simple loop
While a<100: a,b = b,a+b print(a)Copy the code
The running results of the program are as follows:
This code implements the Fibonacci sequence in a non-recursive way. Next, I implement the Fibonacci sequence as a generator
Import sys def Fibonacci (n): import sys def Fibonacci (n): import sys def Fibonacci (n): Use the generator (iterator. When the program encounters a yield function, it pauses and saves all current information -- # and returns the yield value. Then when the program runs to the next() function, A,b = b,a+b count = count + 1f = Fibonacci (10) #f is an iterator, generated by the generator, 10 is the end of the recursive signal print(next(f),end=" ") except: sys.exit()Copy the code
The running results of the program are as follows:
What is an iterator?
Iteration is one of Python’s many powerful features. It’s a way of accessing elements of a collection, and it’s a way of remembering the location of objects to traverse. It’s similar to the enumerate keyword in the for loop, except that enumerate is a function for remembering indices for arrays, tuples, lists, etc. But the iterator starts at the first element in the collection and goes all the way to the last. The iterator can only go forward and never back.
Iterators have two methods:
- iter()
- next()
Iterators can be created for strings, lists, tuples, and dictionaries. Here are a few simple examples
List = [2,1,4,6] It = list for I in It: print(I,end=" ")Copy the code
Similarly, you can use the next() function
Import sys list = [11,22,33,454]It = iter(list) while True: try: print(next(It)) except: sys.exit()Copy the code
Three, what is decorator?
A decorator can add additional functionality to a function without changing the local structure of the function. Here’s an example
1. Let’s print a sentence with a function
Def sayHi(): print("Hi,man") #sayHi() #sayHi()Copy the code
Now we want to be able to type a sentence before the “sayHi” function. This way of dynamically adding functionality during code execution is called a decorator. Let’s change the code above:
2. Change it
def sayHello(func):
def inner():
print("Hello,I am Gorit")
return func
return inner()
def sayHi():
print("Hi,man")
x = sayHello(sayHi)
s()
Copy the code
It doesn’t look very good, and we have to use it every time we add a feature
x = sayHello(sayHi)
s()
Copy the code
For such cases, Python provides syntax sugar — “@” — so that we can easily print the statements we want
3. With the addition of syntax sugar, the program becomes simpler
def sayHello(func):
def inner():
print("Hello,I am Gorit")
return func
return inner()
@sayHellod
ef sayHi():
print("Hi,man")
sayHi()
Copy the code
In the above code, we define a sayHello(func) function. SayHello takes the argument func, inserts an inner() function inside it, prints (” Hello,I am Gorit “), and returns the func argument, Then we return the inner function, and we find that this is a closed function, also known as a closure function
Then we could sayHi () before joining @ sayHello, its purpose to make the program run here will call a decorator functional test interview treasure dian “sayHello”, and in the decoration function sayHi passed as a parameter, the “sayHi” is pointing to the sayHello. Inner address, When you then call sayHi, you actually call sayHello.inner