One, iterator
Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files
Characteristics:
- The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method
- A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
- You can't go back halfway through the interview.
- Facilitates recycling of large data sets, saving memory
1 #iter ()2 3s ='where is democracy?' #S is a Iterable object, it has a __getitem__ () method4 5it = ITER (s)#It is a iterator object that has __next__ () and __iter__ () methods6 7 Print(s)8 Print(it.__next__())9 Print(it.__next__())Ten Print(it.__next__()) One Print(it.__next__())
Output Result:
Where is democracy?
People
Main
In
Which
Generator generator
Definition: When a function call returns an iterator, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator
1 defCash_money (a):2 whileA >0:3A-= 1004 yield 'Take away' 5 Print('come and get the money again! ')6ATM = Cash_money (500)7 Print(ATM.__next__())8 Print('I am the black sheep, all spent')9 Print(ATM.__next__())Ten Print('I don't have a penny left .') One Print(ATM.__next__())
Role:
The main effect of this yield is that the function can be interrupted, and save the interrupt state, after the interruption, the code can continue to execute, after a period of time can also recall the function, from the last yield of the next sentence to start execution.
In addition, it can realize the effect of concurrency operation in single-threaded case by yield.
Import timedef Consumer (name): print ("%s ready to eat Buns!"%name) while True: baozi = yield print ("Bun [%s] came, was [ %s] Eat! "% (Baozi,name)) def producer (name): C = Consumer (' A ') c2 = consumer (' B ') c.__next__ () c2.__ next__ () print ("Lao Tzu started to make buns!") For I in range (3): time.sleep (1) print ("Made 2 buns!") C.send (i) #send方法是传送yield值 c2.send (i) Producer (' Xigang ')
Second, the decoration device
def login (func): def Inner (ARG): print (' validation ') return func (ARG) return inner@login #相当于: TV = Login (TV) def TV (arg1): print (' welcom to TV page ') def movie (): print (' movie ') Movie () TV (1)
Results:
Movie
Verify
Welcom to TV page '
With parametric adorner
def before (Request,kargs): print (' before ') def after (Request,kargs): print (' after ') def Filter (Before_func, After_func): def outer (main_func): def wrapper (Request,kargs): Before_result = before_func (Request, Kargs) if (before_result! = None): return before_result; Main_result = Main_func (Request,kargs) if (main_result! = None): return main_result; After_result = After_func (Request,kargs) if (after_result! = None): return after_result; Return wrapper return Outer@filter (before, after) def index (Request,kargs): print (' index ') index
Third, recursion
Characteristics
Recursive algorithm is a direct or indirect process to invoke its own algorithm. In the computer programming, the recursive algorithm is very effective to solve a large class of problems, it often makes the description of the algorithm concise and easy to understand. The recursive algorithm solves the problem characteristic: (1) recursion is to call itself in a procedure or function. (2) When using a recursive strategy, there must be a definite recursive end condition called a recursive exit. (3) Recursive algorithm is usually very concise, but the recursive algorithm is less efficient in solving problems. Therefore, the recursive algorithm is generally not advocated for the design of the program. (4) in the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on to store. Too many recursion times can cause stack overflow and so on. Therefore, the recursive algorithm is generally not advocated for the design of the program.
Python Learning Note day4