日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python装饰器函数传参

發布時間:2025/3/11 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python装饰器函数传参 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python裝飾器函數傳參?

裝飾器

裝飾器是一個返回函數的高階函數。

  • 裝飾器常見用法:
    打印日志
def logger(func):def wrapper(*args, **kw):print 'do {}'.format(func.__name__)func(*args, **kw)print 'finish'return wrapper@logger def add(x,y):print '{} + {} = {}'.format(x,y,x+y)add(3,5)

在函數執行前,打印一行日志do...;函數執行結束,打印一行日志finish。執行結果如下:

do add 3 + 5 = 8 finish

計算時間

import timedef timer(func):def wrapper(*args, **kw):t1 = time.time()func(*args,**kw)t2 = time.time()cost_time = t2 - t1print 'cost time: {} s'.format(cost_time)return wrapper@timer def cost_time(sleep_time):time.sleep(sleep_time)cost_time(10)
  • 帶參數的函數裝飾器
def say_hello(country):def wrapper(func):def decorate(*args,**kw):if country == 'en':print 'hello'elif country == 'usa':print 'hi'else:returnfunc(*args,**kw)return decoratereturn wrapper@say_hello("usa") def usa():print 'i am from usa'@say_hello("en") def en():print 'i am from england'usa() print '----------------------' en()

裝飾器本身是一個函數,使用兩層嵌套傳參,執行結果如下:

hi i am from usa ---------------------- hello i am from england
  • 不帶參數的類裝飾器
    基于類裝飾器的實現,必須實現__call__和__init__兩個內置函數。
    __init__ :接收被裝飾函數
    __call__ :實現裝飾邏輯
class logger(object):def __init__(self,func):self.func = funcdef __call__(self,*args,**kwargs):print 'the function {}() is running...'\.format(self.func.__name__)return self.func(*args,**kwargs)@logger def say(something):print 'say {}!'.format(something)say('hello')

運行結果如下:

the function say() is running... say hello!
  • 帶參數的類裝飾器
    帶參數和不帶參數的類裝飾器有很大的不同。
    __init__ :不再接收被裝飾函數,而是接收傳入參數
    __call__ :接收被裝飾函數,實現裝飾邏輯
class logger(object):def __init__(self,level='INFO'):self.level = leveldef __call__(self,func):def wrapper(*args,**kwargs):print '{level}: the function {func} () is running...'\.format(level=self.level, func=func.__name__)func(*args,**kwargs)return wrapper@logger(level='WARNING') def say(something):print 'say {}!'.format(something)say('hello')

運行結果如下:

WARNING: the function say () is running... say hello!

函數的參數

  • 位置參數
def power(x, n):s = 1while n > 0:n = n - 1s = s * xreturn s

power(x, n)函數有兩個參數:x和n,這兩個參數都是位置參數,調用函數時,傳入的兩個值按照位置順序依次賦值給參數x和n。

  • 默認參數
def power(x, n=2):s = 1while n > 0:n = n - 1s = s * xreturn s

power(x, n)函數有兩個參數:x和n,如果想在不傳入n值時,默認計算x的平方,此時可以將n設為默認值2。

  • 可變參數(*args)
def function(f_arg, *args):print f_arg, type(f_arg)print args, type(args) nums = ['a','b','c'] function(1,2,*nums)

定義可變參數時,需要在參數前面加一個*號,可變參數的個數是可變的。在函數內部,參數*args接收到的是一個tuple。輸出結果如下:

1 <type 'int'> (2, 'a', 'b', 'c') <type 'tuple'>
  • 關鍵字參數(**kwargs)
def person(name,age,**kwargs):print 'name:',name,'age:',age,'other:',kwargs,type(kwargs)person('mark',30,city='shanghai')

**kwargs允許將不定長度的鍵值對,作為參數傳遞給一個函數,關鍵字參數在函數內部自動組裝為一個dict。輸出結果如下:

name: mark age: 30 other: {'city': 'shanghai'} <type 'dict'>
  • 將函數作為參數傳遞給另一個函數
def hi():return 'hi friends'def function(func):print 'just test'print func()function(hi)

function()函數將hi函數作為參數接收,輸出結果如下:

just test hi friends

time模塊

  • 獲取當前時間
>>> time.localtime() time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=14, tm_min=31, tm_sec=18, tm_wday=2, tm_yday=233, tm_isdst=0)
  • 獲取格式化的時間
>>> time.ctime() 'Wed Aug 21 14:51:28 2019' >>> time.asctime() 'Wed Aug 21 14:51:34 2019'
  • 格式化日期
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) '2019-08-21 14:35:02' >>> time.strftime('%a %b %d %H:%M:%S %Y',time.localtime()) 'Wed Aug 21 14:36:09 2019'
  • 計算運行時間
import time start = time.time() time.sleep(2) end = time.time() print end-start

總結

以上是生活随笔為你收集整理的python装饰器函数传参的全部內容,希望文章能夠幫你解決所遇到的問題。

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