欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3.5裝飾器原理及應(yīng)用實(shí)例詳解

 更新時(shí)間:2019年04月30日 09:47:04   作者:loveliuzz  
這篇文章主要介紹了Python3.5裝飾器原理及應(yīng)用,結(jié)合具體實(shí)例形式詳細(xì)分析了Python3.5裝飾器的概念、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Python3.5裝飾器原理及應(yīng)用。分享給大家供大家參考,具體如下:

1、裝飾器:

(1)本質(zhì):裝飾器的本質(zhì)是函數(shù),其基本語法都是用關(guān)鍵字def去定義的。

(2)功能:裝飾其他函數(shù),即:為其他函數(shù)添加附加功能。

(3)原則:不能修改被裝飾的函數(shù)的源代碼,不能修改被裝飾的函數(shù)的調(diào)用方式。即:裝飾器對(duì)待被修飾的函數(shù)是完全透明的。

(4)簡(jiǎn)單應(yīng)用:統(tǒng)計(jì)函數(shù)運(yùn)行時(shí)間的裝飾器

import time
#統(tǒng)計(jì)函數(shù)運(yùn)行時(shí)間的磚裝飾器
def timmer(func):
  def warpper(*args,**kwargs):
    strat_time = time.time()
    func()
    stop_time = time.time()
    print("the func run time is %s" %(stop_time-strat_time))
  return warpper
@timmer
def test1():
  time.sleep(3)
  print("in the test1")
test1()
 

運(yùn)行結(jié)果:

in the test1
the func run time is 3.000171661376953

(5)實(shí)現(xiàn)裝飾器知識(shí)儲(chǔ)備:

a、函數(shù)即“變量”

b、高階函數(shù)

c、函數(shù)嵌套

d、高階函數(shù)+嵌套函數(shù)==》裝飾器

2、裝飾器知識(shí)儲(chǔ)備——函數(shù)即“變量”

定義一個(gè)函數(shù),相當(dāng)于把函數(shù)體賦值給這個(gè)函數(shù)名。

Python解釋器如何回收變量:采用引用計(jì)數(shù)。當(dāng)引用有沒有了時(shí)(門牌號(hào)不存在),變量就被回收了。

函數(shù)的定義也有內(nèi)存回收機(jī)制,與變量回收機(jī)制一樣。匿名函數(shù)沒有函數(shù)名,就會(huì)被回收。

變量的使用:先定義再調(diào)用,只要在調(diào)用之前已經(jīng)存在(定義)即可;函數(shù)即“變量”,函數(shù)的使用是一樣的。

函數(shù)調(diào)用順序:其他的高級(jí)語言類似,Python 不允許在函數(shù)未聲明之前,對(duì)其進(jìn)行引用或者調(diào)用

下面的兩段代碼運(yùn)行效果一樣:

def bar():
  print("in the bar")
def foo():
  print("in the foo")
  bar()
foo()
#python為解釋執(zhí)行,函數(shù)foo在調(diào)用前已經(jīng)聲明了bar和foo,所以bar和foo無順序之分
def foo():
  print("in the foo")
  bar()
def bar():
  print("in the bar")
foo()


運(yùn)行結(jié)果:

in the foo
in the bar
in the foo
in the bar

注意:python為解釋執(zhí)行,函數(shù)foo在調(diào)用前已經(jīng)聲明了bar和foo,所以bar和foo無順序之分

原理圖為:

3、裝飾器知識(shí)儲(chǔ)備——高階函數(shù)

滿足下列其中一種即可稱之為高階函數(shù):

a、把一個(gè)函數(shù)名當(dāng)做實(shí)參傳遞給另一個(gè)函數(shù)(在不修改被裝飾函數(shù)的情況下為其添加附加功能)

b、返回值中包含函數(shù)名(不修改函數(shù)的調(diào)用方式)

(1)高階函數(shù)示例:

def bar():
  print("in the bar")
def test1(func):
  print(func)  #打印門牌號(hào),即內(nèi)存地址
  func()
test1(bar)   #門牌號(hào)func=bar

運(yùn)行結(jié)果:

<function bar at 0x00BCDFA8>
in the bar

(2)高階函數(shù)的妙處——把一個(gè)函數(shù)名當(dāng)做實(shí)參傳遞給另一個(gè)函數(shù)(在不修改被裝飾函數(shù)的情況下為其添加附加功能)

import time
def bar():
  time.sleep(3)
  print("in the bar")
#test2在不修改被修飾函數(shù)bar的代碼時(shí)添加了附加的及時(shí)功能
def test2(func):
  start_time = time.time()
  func()   #run bar
  stop_time = time.time()
  print("the func run time is %s " %(stop_time-start_time))
#調(diào)用方式發(fā)生改變,不能像原來的方法去調(diào)用被修飾的函數(shù)(所以不能實(shí)現(xiàn)裝飾器的功能)
test2(bar)
#bar()


運(yùn)行結(jié)果:

in the bar
the func run time is 3.000171661376953

(3)高階函數(shù)的妙處——返回值中包含函數(shù)名(不修改函數(shù)的調(diào)用方式)

import time
def bar():
   time.sleep(3)
   print("in the bar")
def test3(func):
  print(func)
  return func
bar = test3(bar)
bar()  #run bar


運(yùn)行結(jié)果:

<function bar at 0x00BADFA8>
in the bar

4、裝飾器知識(shí)儲(chǔ)備——嵌套函數(shù)

#函數(shù)嵌套
def foo():
  print("in the foo")
  def bar():  #bar函數(shù)具有局部變量的特性,不能在外部調(diào)用,只能在內(nèi)部調(diào)用
    print("in the bar")
  bar()
foo()

運(yùn)行結(jié)果:

in the foo
in the bar

裝飾器應(yīng)用——模擬網(wǎng)站登錄頁面,訪問需要認(rèn)證登錄頁面

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#模擬網(wǎng)站,訪問頁面和部分需要登錄的頁面
import timer
user,passwd = "liu","liu123"
def auth(func):
  def wrapper(*args,**kwargs):
    username = input("Username:").strip()
    password = input("Password:").strip()
    if username == user and password == passwd:
      print("\033[32;1mUser has passed authentication!\033[0m")
      res = func(*args,**kwargs)
      print("-----after authentication---")
      return res
    else:
      exit("\033[31;1mInvalid username or password!\033[0m")
  return wrapper
def index():
  print("welcome to index page!")
@auth
def home():
  print("welcome to index home!")
  return "from home"
@auth
def bbs():
  print("welcome to index bbs!")
#函數(shù)調(diào)用
index()
print(home())
bbs()
 

運(yùn)行結(jié)果:

welcome to index page!
Username:liu
Password:liu123
User has passed authentication!
welcome to home page!
-----after authentication---
from home
Username:liu
Password:liu123
User has passed authentication!
welcome to bbs page!
-----after authentication---

裝飾器帶參數(shù)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#模擬網(wǎng)站,訪問頁面和部分需要登錄的頁面,多種認(rèn)證方式
import timer
user,passwd = "liu","liu123"
def auth(auth_type):
  print("auth func:",auth_type)
  def outer_wrapper(func):
    def wrapper(*args, **kwargs):
      print("wrapper func args:",*args, **kwargs)
      if auth_type == "local":
        username = input("Username:").strip()
        password = input("Password:").strip()
        if username == user and password == passwd:
          print("\033[32;1mUser has passed authentication!\033[0m")
          #被裝飾的函數(shù)中有返回值,裝飾器中傳入的參數(shù)函數(shù)要有返回值
          res = func(*args, **kwargs)  #from home
          print("-----after authentication---")
          return res
        else:
          exit("\033[31;1mInvalid username or password!\033[0m")
      elif auth_type == "ldap":
        print("ldap....")
    return wrapper
  return outer_wrapper
def index():
  print("welcome to index page!")
@auth(auth_type="local")    #利用本地登錄 home = wrapper()
def home():
  print("welcome to home page!")
  return "from home"
@auth(auth_type="ldap")    #利用遠(yuǎn)程的ldap登錄
def bbs():
  print("welcome to bbs page!")
#函數(shù)調(diào)用
index()
print(home())   #wrapper()
bbs()

運(yùn)行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 利用Python生成隨機(jī)驗(yàn)證碼詳解

    利用Python生成隨機(jī)驗(yàn)證碼詳解

    怎么用python繞驗(yàn)證碼是個(gè)令人頭禿的事情,這篇文章將為大家詳細(xì) 介紹如何利用Python生成隨機(jī)的驗(yàn)證碼,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起試試
    2022-01-01
  • 詳解tensorflow實(shí)現(xiàn)遷移學(xué)習(xí)實(shí)例

    詳解tensorflow實(shí)現(xiàn)遷移學(xué)習(xí)實(shí)例

    本篇文章主要介紹了詳解tensorflow實(shí)現(xiàn)遷移學(xué)習(xí)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • python爬蟲之爬取百度音樂的實(shí)現(xiàn)方法

    python爬蟲之爬取百度音樂的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python爬蟲之爬取百度音樂的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 一文帶你了解python中的多態(tài)性

    一文帶你了解python中的多態(tài)性

    多態(tài)性是 OOP 的一個(gè)特征,這意味著一個(gè)名稱可以具有不同的功能, 這篇文章主要為大家詳細(xì)介紹了如何在 Python 中實(shí)現(xiàn)多態(tài)性,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-11-11
  • 詳解Python之可迭代對(duì)象,迭代器和生成器

    詳解Python之可迭代對(duì)象,迭代器和生成器

    這篇文章主要為大家介紹了Python之可迭代對(duì)象,迭代器和生成器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Anaconda修改默認(rèn)虛擬環(huán)境安裝位置的方案分享

    Anaconda修改默認(rèn)虛擬環(huán)境安裝位置的方案分享

    新安裝Anaconda后,在創(chuàng)建環(huán)境時(shí)環(huán)境自動(dòng)安裝在C盤,但是C盤空間有限,下面這篇文章主要給大家介紹了關(guān)于Anaconda修改默認(rèn)虛擬環(huán)境安裝位置的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • python第三方庫pygame的使用詳解

    python第三方庫pygame的使用詳解

    這篇文章主要介紹了python第三方庫pygame的使用,pygame一般用來做游戲,在這需要注意在使用pygame提供的功能之前,需要調(diào)用init方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 使用Python實(shí)現(xiàn)橋接模式的代碼詳解

    使用Python實(shí)現(xiàn)橋接模式的代碼詳解

    橋接模式是一種結(jié)構(gòu)型設(shè)計(jì)模式,它將抽象部分與其實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化,本文將給大家介紹如何使用Python實(shí)現(xiàn)橋接模式,需要的朋友可以參考下
    2024-02-02
  • python實(shí)現(xiàn)復(fù)制整個(gè)目錄的方法

    python實(shí)現(xiàn)復(fù)制整個(gè)目錄的方法

    這篇文章主要介紹了python實(shí)現(xiàn)復(fù)制整個(gè)目錄的方法,涉及Python中shutil模塊的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • opencv繪制矩形和圓的實(shí)現(xiàn)

    opencv繪制矩形和圓的實(shí)現(xiàn)

    本文主要介紹了opencv繪制矩形和圓的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論