Function closures, decorators, iterator

Function closure

Closure: closing a closed (function of the internal function), is a package comprising (a variable function of the inner rather than the outer scope reference global scope), the closure means is: action function rather than global internal function outside scope the reference field

import requests

def func(url):
    def get_res():
        res=requests.get(url)
        res.encoding='utf8'
        print(res.text)
    return get_res
# baidu=func('https://www.baidu.com')
# baidu()
taobao=func('https://www.taobao.com/')
taobao()
Two kinds of transmission parameters as a function of the parameters

1, in the form of parameters

def func(x):
    print(x)
func(1)
func(1)
func(1)
#1
#1
#1

2, contracted function

def outter(x):
    x=1
    def inner():
        print(x)
    return inner
f=outter(1)
f()
f()
f()
#1
#1
#1

Decorator

Refers to a tool unit, the program and the function is a function of the tool is provided, it refers to a decorator to add additional functionality to the object to be decorated, thus defining decorator is to define a function, but the function is a function to add additional functionality to the other function

requires attention

Decorator itself is actually any object can be called

Ornamented objects can be any callable objects

Achieve decorator must follow two principles

Does not modify the source code of an object to be decorated

Call does not modify the manner of decorative objects

import time
def index():
    print('hello, world')
    # return index  ##再次返回调用一次
# index()

def dice(func):   ##装饰器

    def f1():     ##装饰的功能
        star=time.time()
        func()
        time.sleep(1)
        end=time.time()
        print(end-star)
    return f1
index=dice(index)
index()
#加强版
import time
def index(x,a=1):
    print('x',x)
    print('a',a)
    print('hello wrold')
    return 123

def deco(func):
    def f1(*args,**kwargs):
        print('args:',args)
        print('kwargs:',kwargs)
        start=time.time()
        res=func(*args,**kwargs)
        end=time.time()
        return res
    return f1

index=deco(index)
res=index(10)
print(res)
##装饰器模板
def login(func):
    def inner(*args,**kwargs):
        user=input('输入用户名')
        pwd=input('输入密码')
        if user=='lzs'and pwd=='1234':
            print('登陆成功')
            res = func(*args, **kwargs)
            return res
        else:
            print('登陆失败')

    return inner

@login
def shopping():
    print('欢迎购物')
shopping()
##三层装饰器:给双层装饰器加参数
def auth(engine):
    def login(func):
        def inner(*args, **kwargs):
            if engine == 'flie':
                user = input('输入用户名')
                pwd = input('密码')
                if user == 'lzs' and pwd == '1234':
                    print('登陆成功')
                    res = func(*args, **kwargs)
                    return res
                else:
                    print('登陆失败')
            elif engine == 'db':
                print('账号来自数据库')
        return inner

    return login

@auth('flie')
def shopping():
    print('shopping')
    # login=auth('flie')
    # shopping=login(shopping)
shopping()

Guess you like

Origin www.cnblogs.com/lzss/p/11401990.html