码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器的补充(叠加多个装饰器,有参装饰器,三元表达式,生成式,匿名函数)

时间:2018-11-30 21:47:12      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:ase   max   pwd   top   赋值   数据   ret   you   success   

1,叠加多个装饰器

  1),加载顺序(outter函数的调用顺序):自下而上

  2),执行顺序(wrapper函数的执行顺序):自上而下

def outter1(func1): #func1=wrapper2的内存地址
    print(加载了outter1)
    def wrapper1(*args,**kwargs):
        print(执行了wrapper1)
        res1=func1(*args,**kwargs)
        return res1
    return wrapper1

def outter2(func2): #func2=wrapper3的内存地址
    print(加载了outter2)
    def wrapper2(*args,**kwargs):
        print(执行了wrapper2)
        res2=func2(*args,**kwargs)
        return res2
    return wrapper2

def outter3(func3): # func3=最原始的那个index的内存地址
    print(加载了outter3)
    def wrapper3(*args,**kwargs):
        print(执行了wrapper3)
        res3=func3(*args,**kwargs)
        return res3
    return wrapper3



@outter1 # outter1(wrapper2的内存地址)======>index=wrapper1的内存地址
@outter2 # outter2(wrapper3的内存地址)======>wrapper2的内存地址
@outter3 # outter3(最原始的那个index的内存地址)===>wrapper3的内存地址
def index():
    print(from index)

 

import time


#时间统计函数
def timmer(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        stop = time.time()
        print(run %s % (stop - start))
        return res

    return wrapper


#账户登录验证函数
def duth(func):
    def wrapper(*args, **kwargs):
        name = input(please input your name:)
        pwd = input(please input your password:)
        if name == egom and pwd == 123:
            print(login successful)
            res = func(*args, **kwargs)
            return res
        else:
            print(your name or password error)
    return wrapper


@timmer
@duth
#装饰器将下面函数的函数名当做参数,然后将运行的结果在赋值给原函数index

#
def index():
    time.sleep(1)
    print(from index)


index()

 

 

2,有参装饰器

current_user = {user:None}

def duth(engine=file):
    def outter(func):
        def wrapper(*args,**kwargs):

            #确认current字典内是否有存在的数据
            if current_user[user] is not None:
                res = func(*args,**kwargs)
                return res

            name = input(your name :).strip()
            password = input(your password :).strip()

            #基于‘file‘文件认证
            if engine == file:
                if name == egon and password == 123:
                    print(login successful)
                    current_user[user]=name
                    res=func(*args,**kwargs)
                    return res
                else:
                    print(your name or password error)

            elif engine == mysql :
                print(基于mysql认证)

            elif engine == ldap :
                print(基于ldap认证)

            else:
                print(hello)

        return wrapper
    return outter

 

3,三元表达式

def max2(x,y):
    if x > y:
        return x
    else:
        return y

res=max2(10,20)

x=10
y=20

# res=x if x > y else y
# print(res)


res=OK if False else No
print(res)

 

4,生成式

  列表生成式

l=[]
for i in range(10):
    if i > 4:
        l.append(i**2)



l=[i**2 for i in range(10) if i > 4]
print(l)

 

names=[egon,alex_sb,kevin_sb,hxx_sb,cxx_sb]
sbs=[]
for name in names:
    if name.endswith(sb):
        sbs.append(name)

sbs
=[name.upper() for name in names if name.endswith(sb)]

 

 

  字典生成器

res={i:i**2 for i in range(10) if i > 3}
print(res)


print({i for i in hello})

 

 5,匿名函数

 

装饰器的补充(叠加多个装饰器,有参装饰器,三元表达式,生成式,匿名函数)

标签:ase   max   pwd   top   赋值   数据   ret   you   success   

原文地址:https://www.cnblogs.com/kp1995/p/10046610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!