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 time

def timer(func):
	def wrapper(*args, **kw):
		t1 = time.time()
		func(*args,**kw)
		t2 = time.time()

		cost_time = t2 - t1
		print '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:
				return
			func(*args,**kw)
		return decorate
	return 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 = func

	def __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 = level

	def __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 = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

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

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

power(x, n)函數有兩個參數:xn,如果想在不傳入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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM