日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 阿狸的进阶之路(4)

發布時間:2023/12/10 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 阿狸的进阶之路(4) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

裝飾器

?

#1、開放封閉原則:對擴展開放,對修改是封閉
#2、裝飾器:裝飾它人的,器指的是任意可調用對象,現在的場景裝飾器-》函數,被裝飾的對象也是-》函數
#原則:1、不修改被裝飾對象的源代碼 2、不修改被裝飾對象的調用方式
#裝飾器的目的:在遵循1,2的前提下為被裝飾對象添加上新功能

?(1)無參數類型

import time def outer(func):def inner():time.sleep(1)print("hello")func()return inner

def bar():print('world')

?(2)有參數類型

# 有參裝飾器 import timedef auth2(engine='file'):def auth(func): # func=indexdef inner(*args,**kwargs):if engine == 'file':name=input('name>>: ').strip()password=input('password>>: ').strip()if name == 'egon' and password == '123':print('login successful')return func(*args,**kwargs)else:print('login err')elif engine == 'mysql':print('mysql auth')elif engine == 'ldap':print('ldap auth')else:print('engin not exists')return innerreturn auth@auth2(engine='mysql') #@auth #index=auth(index) #index=inner def index(name):time.sleep(1)print('welecome %s to index' %name)return 1111res=index('egon') #res=inner('egon') print(res)

(3)并列裝飾器

import time def timmer(func):def inner(*args,**kwargs):start=time.time()res=func(*args,**kwargs)stop=time.time()print('run time is %s' %(stop-start))return resreturn innerdef auth2(engine='file'):def auth(func): # func=indexdef inner(*args,**kwargs): # 一致if engine == 'file':name=input('name>>: ').strip()password=input('password>>: ').strip()if name == 'egon' and password == '123':print('login successful')res = func(*args,**kwargs) #一致return reselse:print('login err')elif engine == 'mysql':print('mysql auth')elif engine == 'ldap':print('ldap auth')else:print('engin not exists')return innerreturn auth@auth2(engine='file') @timmer def index(name):time.sleep(1)print('welecome %s to index' %name)return 1111res=index('egon') print(res)

(4)

from functools import wraps import time def timmer(func):@wraps(func)def inner(*args,**kwargs):start=time.time()res=func(*args,**kwargs)stop=time.time()print('run time is %s' %(stop-start))return res# inner.__doc__=func.__doc__# inner.__name__=func.__name__return inner@timmer def index(name): #index=inner'''index 函數。。。。。'''time.sleep(1)print('welecome %s to index' %name)return 1111res=index('egon') print(res)print(help(index))

?

轉載于:https://www.cnblogs.com/taozizainali/p/8202361.html

總結

以上是生活随笔為你收集整理的python 阿狸的进阶之路(4)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。