1. 程式人生 > >裝飾器的補充(疊加多個裝飾器,有參裝飾器,三元表示式,生成式,匿名函式)

裝飾器的補充(疊加多個裝飾器,有參裝飾器,三元表示式,生成式,匿名函式)

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,匿名函式