This is the 9th day of my participation in the August Wen Challenge.More challenges in August
@[TOC]
preface
Now that you have some understanding of functions, let’s take a look at generators and decorators
The generator
Generators are supposed to be used with iterators, but for one reason or another, I’ll leave it there
A generator is a function that returns an iterator. We’ve already said what an iterator is.
So a generator is essentially a function
However, instead of using return, generators use yield as the return keyword.
In an iterator, when you use it once, the computer remembers that you’ve already used it once, and the next time you use it, it picks up where you left off
In generators, when a function is called, when yield is encountered, the computer stops calling the function and remembers the current value so that it can be used the next time it is called
Why use
Generators are generally used for large memory footprint and large data
Such as this
for i in range(1000Arunachal Pradesh100000) :print(i)
Copy the code
The data is large, and when our machine iterates over it, it consumes a lot of memory to iterate over it, and if there were a way to iterate over the loop while I’m using it, it would save a lot of memory
format
def Xxx() :codeyield x
Copy the code
Make the above example a simple generator
def Scq() :
for i in range(1000Arunachal Pradesh100000) :yield i
sc = Scq()
print(next(sc))
print(next(sc))
print(next(sc))
print(next(sc))
Copy the code
Running results:
As you can see, when we use it, we are given the corresponding value, and if we just iterate over that value, we will wait a long time to finish.
Nested function
You’ve seen so much nesting, it doesn’t bother you to see nested functions, does it
Go straight to code
def Out() :
def Int() :
print("I'm on the inside.")
print("I'm from the outside.")
return Int()
Out()
Copy the code
Running results:
You don’t usually use this, but this is just a way to prepare your decorator, so you don’t have to look at your decorator and be confused
The return value is to call the inner function, otherwise the inner function will not be used
A decorator
Let me just say, the decorator is very 666
A decorator is something that decorates a function.
Usage scenarios
This code of our company has been on the market and cannot be modified, but the customer has raised requirements for us to add functions, what should we do at this time?
There seems to be no solution, because I can’t change it, and you want me to add features, how can I do that?
It can be done! This method is a decorator, which can add functionality to the code without using it.
use
# To make it easier to understand, I use Chinese
defExternal function (a) :
Define a function with parameters that are used to refer to core code
defInternal function ():
# Define internal functions, using parameters to call core code
a()
# above the parameter is appended above, below the parameter is appended below
print("Added to the core code function.")
returnInternal function# return a reference to the inner function, not the inner function
@ External function ()
defCore code ():
print("I'm core code.") Core code ()Copy the code
References to internal functions are mentioned here
Without parentheses, a reference is a set of memory addresses that point to an inner function, and parentheses, a call to an inner function, uses the inner function, just like a nested function.
conclusion
So much for now, it’s getting late.
Interest is the best teacher, persistence is the invariable truth. Learn not to be impatient, one step at a time, step forward steadily. Make a little progress every day, and over a long period of time, you will find that you have become very strong.
I am Bu Xiaochan, a cute new self-study, follow me every day a little bit of progress!
This is enough for the time being, so be on your way