python中的装饰器迭代器生成器

装饰器:

定义:本质是函数(装饰其它函数) 为其它函数添加附加功能

原则: 1 不能修改被装饰函数源代码    2 不修改被装饰函数调用方式

实现装饰器知识储备:

1 函数即‘’变量‘’   

2 高阶函数 

  a 把一个函数名当实参传给另外一个函数(在不修改被装饰函数的情况下 为其添加功能)

  b 返回值中包含函数名(不修改函数的调用方式)

3嵌套函数

 

高阶函数 + 嵌套函数 =》 装饰器

 

简单版高阶函数

import time
def bar():
    time.sleep(3)
    print('in the bar')
def test2(func):
    print(func)
    return func

bar=test2(bar)
bar()  #run bar     

  简单版 嵌套函数

 

def foo():
    print('in the foo')
    def bar():
        print('in the bar')

    bar()
foo()

  装饰器 = 高阶函数 + 嵌套函数

import time
def timmer(func):
    def warpper(*args,**kwargs):
        start_time=time.time()
        func()
        stop_time=time.time()
        print('the func run time is %s' %(stop_time-start_time))
    return warpper

@timmer
def test1():
    time.sleep(3)
    print('in the test1')

test1()

  修改版装饰器 任意传参数版:

import time
def timer(func): #timer(test1)  func=test1
    def deco(*args,**kwargs):
        start_time=time.time()
        func(*args,**kwargs)   #run test1()
        stop_time = time.time()
        print("the func run time  is %s" %(stop_time-start_time))
    return deco
@timer  #test1=timer(test1)
def test1():
    time.sleep(1)
    print('in the test1')

test1()

  修改版装饰器 任意传参数版2

# Author:Lance
import time
def timer(func): #timer(test1)  func=test1
    def deco(*args,**kwargs):
        start_time=time.time()
        r =func(*args,**kwargs)   #run test1()
        stop_time = time.time()
        print("the func run time  is %s" %(stop_time-start_time))
        return r
    return deco
@timer  #test1=timer(test1)
def test1():
    time.sleep(1)
    print('in the test1')
    return "你是大撒B"

@timer # test2 = timer(test2)  = deco  test2(name) =deco(name)
def test2(name,age):
    print("test2:",name,age)

a =test1()
print(a)
test2("abc",123)

输出结果:
in the test1
the func run time  is 1.0000011920928955
你是大撒B
test2: abc 123
the func run time  is 0.0

  加强版 装饰器

import time
user,passwd = 'lance','123456'
def auth(auth_type):
    print("auth func:",auth_type)
    def outer_wrapper(func):
        def wrapper(*args, **kwargs):
            print("wrapper func args:", *args, **kwargs)
            if auth_type == "local":
                username = input("Username:").strip()
                password = input("Password:").strip()
                if user == username and passwd == password:
                    print("\033[32;1mUser has passed authentication\033[0m")
                    res = func(*args, **kwargs)  # from home
                    print("---after authenticaion ")
                    return res
                else:
                    exit("\033[31;1mInvalid username or password\033[0m")
            elif auth_type == "ldap":
                print("搞毛线ldap,不会。。。。")

        return wrapper
    return outer_wrapper

@auth(auth_type="local") # home = wrapper()如果没有语法糖@ 相当于这样的写法home = auth(auth_type='local')(home)
def home():
    print("welcome to home  page")
    return "from home"

print(home()) #wrapper()

输出结果
auth func: local
wrapper func args:
Username:lance
Password:123456
User has passed authentication
welcome to home  page
---after authenticaion 
from home

 

装饰器更高级的用法:请参照 https://www.cnblogs.com/cicaday/p/python-decorator.html

生成器:略

迭代器:略

posted @ 2019-04-26 09:52  Samuel-Leung  阅读(192)  评论(0编辑  收藏  举报