Python 基礎知識 Day4

來源:互聯網
上載者:User

標籤:int   最佳化   author   lap   嵌套   arp   另一個   imm   art   

本節內容

1 迭代器和裝飾器

2 裝飾器

3 Json 和 Pickle資料序列化

4 軟體目錄結構規範

5 作業:ATM項目開發

 

一  裝飾器

1 裝飾器:
2 定義:本質是函數,(裝飾其他函數),就是為其他函數添加附加功能
3 原則:
1-不能修改被裝飾的函數的原始碼
2-不能修改被裝飾的函數的調用方式

實現裝飾器知識儲備:
1.函數即"變數" (定義函數體到記憶體房間,函數體是字串,調用的時候,通過函數名直接調取記憶體的函數體,函數名就是記憶體位址)
2.高階函數
  滿足下面兩個條件之一就是高階函數
  A:把一個函數名當作實參傳給另一個函數(在不修改被裝飾函數原始碼的情況下為其添加功能)
def bar():    print("in the bar")def test1(func):    print(func)    func()test1(bar)              # 此處bar 就是 把 func = bar ,func等於bar了,意思就是fucn 有了記憶體位址,有了函數體

  

import  timedef bar():    time.sleep(3)    print("in the bar")def test1(func):    start_time = time.time()    func()    stop_time = time.time()    print("the func run time is %s "%(stop_time-start_time))    # 這裡就是func 啟動並執行時間test1(bar)

  

  



  B:傳回值中包含函數名(不修改函數的調用方式)
import timedef bar():    time.sleep(3)    print("in the bar")def test2(func):    print(func)    return funcbar = (test2(bar))bar()                   # 這裡實現了未改變原始碼的情況下,增加列印func(其實就是bar的記憶體位址)的記憶體位址的功能                        # 並且未改變源函數的調用方式

  


3.嵌套函數
def foo():    print("in the foo")    def bar():              # 函數的嵌套是 在一個函數的函數體內用def 去申明一個函數(而不是調用函數)        print("in the bar")    bar()

  


高階函數+嵌套函數 = 裝飾器

__author__ = ‘shellxie‘# -*- coding:utf-8 -*-import timedef timmer(fuc):            #未修改原始碼    def warpper (*args,**kwargs):        start_time = time.time()        fuc()        stop_time = time.time()        print("in the fuc run time %s"%(stop_time-start_time))    return warpper@timmer                     #調用裝飾器def test1():                # 一個被裝飾的函數    time.sleep(3)    print("in the test1")test1()

  裝飾器1.0版

import timedef timer(func):                  # 這時候timer(test1) 就是tiemr(func) func = test1    def deco():        start_time = time.time()        func()                    # 這時候是run test1        stop_time = time.time()        print("the func run time %s"%(stop_time-start_time))    return decodef test1():    time.sleep(3)    print( "in the test1")        # 在不改變調用方式以及原始碼的情況下,給源函數增加功能def test2():    time.sleep(3)    print("in the test2")print(timer(test1))test1= (timer(test1))test1()                           # 這時候test1() ------就是執行deco的記憶體位址然後調用

  裝飾器1.0版最佳化

import timedef timer(func):                  # 這時候timer(test1) 就是tiemr(func) func = test1    def deco():        start_time = time.time()        func()                    # 這時候是run test1        stop_time = time.time()        print("the func run time %s"%(stop_time-start_time))    return deco@timer                            # 這一步的意思就是test1 = timer(test1)def test1():    time.sleep(3)    print( "in the test1")        # 在不改變調用方式以及原始碼的情況下,給源函數增加功能@timerdef test2():    time.sleep(3)    print("in the test2")test1()test2()

  

 

Python 基礎知識 Day4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.