1. Generator
With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, create a containing 1 million
The list of elements, not only takes up a lot of storage space, if we just need to access the first few elements, then the vast majority of elements behind the space is wasted
The fee.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This will not create
Create a complete list, saving a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.
There are a number of ways to create a generator. The first method is simple, as long as a list-generated [] is changed to (), a generator is created:
L = [x * x for x in range (10)]
g = (x * x for x in range (10))
The difference between creating L and G is only the outermost [] and (), L is a list, and G is a generator.
We can print out every element of the list directly, but how do we print out every element of generator?
If you want to print one, you can get the next return value of generator with the next () function
When there are no more elements, throw stopiteration errors.
def fib (max):
N, a, b = 0, 0, 1
While n < max:
Print (b)
A, B = B, A + b
n = n + 1
Return ' Done '
def fib (max):
N,a,b = 0,0,1
While n < max:
Yield b
A, B = b,a+b
n + = 1
Return ' Done '
Iterators
We already know that there are several types of data that can be directly applied to a For loop:
A class is a collection of data types, such as list, tuple, dict, set, str, and so on;
A class is generator, including generators and generator function with yield.
These objects that can directly act on a for loop are called an iterative object: Iterable.
You can use Isinstance () to determine whether an object is a Iterable object:
The generator can not only be used for a for loop, but can also be called by the next () function and return the next value
An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator.
From collections Import Iterator
Isinstance ((x for X in range), Iterator)
Generators are iterator objects, but list, dict, and Str are iterable, but not iterator.
To change the list, dict, str, etc. iterable to iterator you can use the ITER () function:
You might ask, why are data types such as list, dict, and str not iterator?
This is because the Python iterator object represents a data stream, and the iterator object can be called by the next () function and continuously return to the next data until no
Throws a stopiteration error when there is data. This data stream can be viewed as an ordered sequence, but we cannot know in advance the length of the sequence, but we can only keep
Using the next () function to calculate the next data on demand, the iterator calculation is lazy and is only calculated when the next data needs to be returned.
Iterator can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.
Summary
All objects that can be used for a for loop are iterable types;
All objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations;
Collection data types such as list, Dict, str, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER () function.
Python's for loop is essentially implemented by calling the next () function continuously
Decorative Device
A function is a higher order function if one of the following conditions is met
1. A function is passed into another function as a parameter
2. The return value of a function contains n functions, n>0
inline function + high-order function + closure = "Decorator
Example one:
Import time
def decorator (func):
def wrapper (*args,**kwargs):
Start=time.time ()
Func (*args,**kwargs)
Stop=time.time ()
print ' Run time is%s '% (Stop-start)
Print timeout
Return wrapper
@decorator
def test (list_test):
For I in List_test:
Time.sleep (0.1)
print '-' *20,i
#decorator (test) (range (10))
Test (range (10))
Example two:
Import time
def timer (timeout=0):
def decorator (func):
def wrapper (*args,**kwargs):
Start=time.time ()
Func (*args,**kwargs)
Stop=time.time ()
print ' Run time is%s '% (Stop-start)
Print timeout
Return wrapper
return decorator
@timer (2)
def test (list_test):
For I in List_test:
Time.sleep (0.1)
print '-' *20,i
Test (range (10))
Python Learning day04