4道Python装饰器练习题
生活随笔
收集整理的這篇文章主要介紹了
4道Python装饰器练习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:編寫3個函數,每個函數執(zhí)行的時間是不一樣的
a = time.localtime()def log_1():print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))def log_2():time.sleep(2)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))def log_3():time.sleep(4)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) log_1() log_2() log_3() """ 2018-3-21 2018-3-21 2018-3-21 """二、編寫裝飾器,為每個函數加上統(tǒng)計運行時間的功能
import time def timmer(func):def inner():start_time = time.time()func()wait_time = time.time() - start_timeprint("%s 運行時間:" % func.__name__, wait_time)return innera = time.localtime()@timmer def log_1():print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday)) @timmer def log_2():time.sleep(2)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) @timmer def log_3():time.sleep(4)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) log_1() log_2() log_3() """ 2018-3-21 log_1 運行時間: 3.0994415283203125e-05 2018-3-21 log_2 運行時間: 2.0049030780792236 2018-3-21 log_3 運行時間: 4.004503965377808 """三、編寫裝飾器,為函數加上認證的功能,即要求認證成功才能執(zhí)行函數
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' user_status = False def login(func):def inner():_username = "alex"_password = "abc!23"global user_statusif user_status is False:username = input("輸入用戶名:")password = input("密碼:")if username == _username and password == _password:print("welcome login...")user_status = Trueelse:print("wrong username or password!")if user_status:func()return innera = time.localtime()def log_1():print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))def log_2():time.sleep(2)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) @login def log_3():time.sleep(4)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))log_1() log_2() log_3() """ 2018-3-21 2018-3-21 輸入用戶名:alex 密碼:abc!23 welcome login... 2018-3-21 """四、編寫裝飾器,為多個函數加上認證功能(用戶的賬戶密碼來源于文件),要求登錄成功一次,后續(xù)的函數都無需再輸入用戶名和密碼。
import os,timeuser_status = False def login(func):file = os.path.exists('user_info.txt')if file is True:file = open(file='user_info.txt', mode='r+', encoding='utf-8')f = file.read()user_info = eval(f)file.close()else:file = open('user_info.txt', mode='w', encoding='utf-8')choice = input("是否注冊用戶?[Y/N]")if choice == 'Y' or choice == 'y':name = input("請輸入新用戶用戶名:")password = input("請輸入新用戶密碼:")user_info = {'name': name, 'password': password}row = str(user_info)file.write(row)file.close()def inner():_username = user_info['name']_password = user_info['password']global user_statusif user_status is False:username = input("輸入用戶名:")password = input("密碼:")if username == _username and password == _password:print("welcome login...")user_status = Trueelse:print("wrong username or password!")if user_status:func()return innera = time.localtime()def log_1():print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))@login def log_2():time.sleep(2)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))@login def log_3():time.sleep(4)print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))log_1() log_2() log_3()""" 是否注冊用戶?[Y/N]Y 請輸入新用戶用戶名:hqs 請輸入新用戶密碼:123 2018-3-21 輸入用戶名:hqs 密碼:123 welcome login... 2018-3-21 2018-3-21 """總結
以上是生活随笔為你收集整理的4道Python装饰器练习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python调用其他文件的类和函数
- 下一篇: python struct模块的使用