python 定義函數 返回值只取其中一個的實現
def test(): return 1,2a, b = test()1 2a, _ = test()1 # 說明 a, _ = test() 中 返回的是一個int類型 后續可直接運算# a, b = test() 返回值是一個元組, 直接運算報錯
補充:Python-裝飾器(被裝飾函數有返回值的情況)
題目:裝飾器實現一個函數計時器比較for循環與map,匿名函數的運算速度
問題:被裝飾的函數有返回值怎么辦
程序內容:import timeimport randomimport stringimport functools#問題:被裝飾的函數有返回值li = [random.choice(string.ascii_letters) for i in range(100)]def timeit(fun): @functools.wraps(fun) #為了保留被裝飾函數的函數名和幫助文檔信息 def wrapper(*args,**kwargs):'''這是一個wrapper函數'''start_time = time.time()res = fun(*args,**kwargs)end_time = time.time()print(’運行時間為:%.6f’ %(end_time - start_time))return res return wrapper@timeitdef fun_list(n): '''這是fun_list函數''' return [i * 2 for i in range(n)]@timeitdef fun_map(n): '''這是fun_map函數''' return list(map(lambda x:x*2,range(n)))print(fun_list(5000))print(fun_map(5000))測試:
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. Python獲取抖音關注列表封號賬號的實現代碼2. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析3. php網絡安全中命令執行漏洞的產生及本質探究4. 解決Python 進程池Pool中一些坑5. php測試程序運行速度和頁面執行速度的代碼6. Python如何讀寫CSV文件7. 三個不常見的 HTML5 實用新特性簡介8. ajax請求添加自定義header參數代碼9. python利用os模塊編寫文件復制功能——copy()函數用法10. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁
