Python 基础知识 Day4

本节内容

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  time

def 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 time
def bar():
    time.sleep(3)
    print("in the bar")


def test2(func):
    print(func)
    return func

bar = (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 time

def 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 time
def 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

def 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 time
def 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")        # 在不改变调用方式以及源代码的情况下,给源函数增加功能
@timer
def test2():
    time.sleep(3)
    print("in the test2")
test1()
test2()

  装饰器2.0------特么的的到底从哪里走到哪里?

__author__ = 'Shell Xie'
# -*- coding: utf-8 -*-
import time
def timer(func):                                         #这是装饰器!!!!!!
    def deco():
        time_start = time.time()
        func()
        time_stop = time.time()
        print("the func run time %s"%(time_stop-time_start))
    return deco
# def foo():
#     print("in the foo")
#     def bar():              # 函数的嵌套是 在一个函数的函数体内用def 去申明一个函数(而不是调用函数)
#         print("in the bar")
#     bar()

def test1():                                            # 这是被装饰的源代码
    time.sleep(2)
    print("in the test1")

def test2():
    time.sleep(2)
    print("in the test2")

timer(test1)
print(timer(test1))
test= timer(test1)
test()


# test1()
# test2()

  

 

posted @ 2017-07-03 23:51  朝雨后  阅读(114)  评论(0编辑  收藏  举报