Decorators + iterator job

1. decorative overlay order execution order and decorators

Decorative order, from the bottom up

Execution order, from the bottom up

2. What is decorated with no parameters have parameters decorator

No reference decorator is no incoming parameters decorator

There are parameters decorator is passed in the parameter decorators to achieve distinction user rights

3. DESCRIPTION iterables, iterator object value and an iterative manner, and for internal circulation principle

Built-containing --iter-- methods are iterables

Iterator object built comprises --iter-- and --next-- iterator object is, index values ​​may not rely on the element within the container is taken out, the value is taken by continuously withdrawn iteration (iteration removed dictionaries , this dictionary is unordered, iterative out that moment python interpreter optimized, becomes orderly);

Also known for loop iteration of the loop, in the back can follow any iterable, when the cycle will be called iterables goods built --iter-- an iterative method to get the object, and then call the iterator's next target the method takes the value assigned to the Item, loop execution cycle is completed, cycle, known to capture stoplteration abnormality, the iteration ends.

4. What are the advantages and disadvantages iterator object is?

Iterator object advantages: iterator object is a data stream represented by calling the next value, the memory storing a value only once. The other value of the loop memory will continue to become more, not enough cause memory problems.

Iterator object Cons: just take the time to stay and a value, the value can only back until depletion, in order to call the previous value, only new calls iter or create a new iteration object, when two cycles simultaneously when an iterator, only one can call success.

5. Write about the results of the implementation of func

def say_hi(func):
    def wrapper(*args, **kwargs):
        print("HI")
        ret = func(*args, **kwargs)
        print("BYE")
        return re
    return wrapper

def say_yo(func):
    def wrapper(*args, **kwargs):
        print("YO")
        return func(*args, **kwargs)
    return wrapper

@say_hi
@say_yo
def func():
      print("Tank & Jason")
func()

##执行结果:hi, yo, tank&jason,  bye;

6. Implement a decorator, the function is called the limiting frequency of, e.g., 10 seconds

import time

def say_hi(func):
    def wrapper(*args,**kwargs):
        print('嗨!')
        res = func(*args, **kwargs)
        print('苍老师:こんにちは/ko n ni chi ha;'
              'はじめまして/ha ji me ma shi te;'
              'よろしくおねがいします/yo ro shi ku o ne ga i shi ma su;')
        return res
    return wrapper

def say_ya(func):
    def wrapper(*args,**kwargs):
        print('哇!')
        res = func(*args, **kwargs)
        return res
    return wrapper

def time_recode(func):
    def wrapper(*args,**kwargs):
        print('请等10秒.....')
        time.sleep(1)
        print('1.')
        time.sleep(1)
        print('2..')
        time.sleep(1)
        print('3...')
        time.sleep(1)
        print('4....')
        time.sleep(1)
        print('5....')
        time.sleep(1)
        print('6.....')
        time.sleep(1)
        print('7.......')
        time.sleep(1)
        print('8........')
        time.sleep(1)
        print('9.........')
        time.sleep(1)
        print('10.........')
        res = func(*args,**kwargs)
        return res
    return wrapper

@say_hi
@time_recode
@say_ya
def func():
    print('苍老师,初次见面,请多关照')

func()

7.ATM + cart
https://www.cnblogs.com/xiaoyuanqujing/articles/11640785.html

Guess you like

Origin www.cnblogs.com/zhulipeng-1998/p/11852347.html