iterator generator

iterator
Builder
Operation
Program to realize ATM shopping mall
Python decorator:
    Definition: The essence is a function, used to decorate other functions and add additional functions to other functions
    Principle: 1. The source code of the decorated function cannot be modified
    2. The calling method of the decorated function cannot be modified
Knowledge base for implementing decorators
1. Functions are variables
2. Higher order functions
3. Nested functions
Higher-order functions + nested functions --> decorators
Nested function example:
    x=0
    def grandpa():
        x=1
        def dad():
            x=2
            def son():
                x=3
                print(x)
            son()
        dad()
    grandpa()
    #The final output is 3
Decorator example:
example1:
    #coding=utf-8
    #Author:colby
    import time
    def timer(fun):#timer(test1)  func=test1
        def col(*args,**kwargs):
            print('in then col1')
            fun(*args,**kwargs)
            print('in then col2')
        return col
    @timer# test2 = timer(test2)  = deco  test2(name) =deco(name)
    def test1(name,age):
        time.sleep(1)
        print(name,age)
    test1('colby add new function!',30)
    operation result:
        in then col1
        in the test1 colby add new function!
        in then col2
example2:
    import time
    user,passwd="colby","abc123"
    def auth(func):
        def wrapper(*args,**kwargs)
            username=input("Username:").strip()
            password=input("Password:").strip()
            if user==username and passwd=password:
                print("\033[32:1mUserhas passed authentication\033[0m")
                #If return is not added here, the function does not return the result
                #func(*args,**kwargs)
                return func(*args,**kwargs)
            else:
                exit("\033[31:1mUserhas passed authentication\033[0m")
        return wrapper
    def index1():
        print("welcome to index page!")
    def index2():
        print("welcome to index page!")
    def index3():
        print("welcome to index page!")
    #function call
    @auth(auth_type="local")
    index1()
    @auth(auth_type="ldap")
    index2()
list comprehension
    [i*2 for i in range(0,10)]
    Already generated, occupying memory space
generator: generator    
    一、(i*2 for i in range(0,10))
    Generate algorithm, no actual data, no memory space
    Summary: 1. Corresponding data will only be generated when called
    2. Only record the current location
    3. There is only one __next__() method, 2.7 is the next() method
    2. Generator with yield
Iterator Iterator
    All objects that can be used in a for loop are of type Iterable
    Anything that can act on the __next__() method is an iterator
    A generator must be an iterator, an iterator is not necessarily a generator
    Iterators such as list, dict, str, etc. belong to iterative objects, through the iter() method
    can be turned into an iterator
    range(10), xrange(10) are also iterators

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686025&siteId=291194637