python開發函數進階:裝飾器

來源:互聯網
上載者:User

標籤:eva   原函數   eval   return   中國   coding   inpu   lob   code   

一,裝飾器本質

閉包函數

功能:就是在不改變原函數調用方式的情況下,在這個函數前後加上擴充功能

 

 

二,設計模式

開放封閉原則

*對擴充是開放的

*對修改是封閉的

 

三,代碼解釋
 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3  4 tag = False 5  6 def login(func): 7     def inner(*args,**kwargs): 8         global tag 9         if tag == False:10             user = input(‘please your username>>>:‘)11             pwd = input(‘please your password>>>:‘)12             f = open(‘aaa‘,‘r‘,encoding=‘utf-8‘)13             for i in f:14                 user_pwd = eval(i)15                 if user == user_pwd[‘name‘] and pwd == user_pwd[‘password‘]:16                     tag = True17             f.close()18         if tag:19             ret = func(*args,**kwargs)20             return ret21     return inner22 23 @login24 def haha(*args,**kwargs):25     print(‘中國動漫‘)26     pass27 28 @login29 def hengheng(*args,**kwargs):30     print(‘美國動漫‘)31     pass32 ##########使用者調用方式33 haha()34 hengheng()

 

python開發函數進階:裝飾器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.