A.装饰器
定义:本质上是函数,(装饰其他函数),就是为其他函数添加附加功能
原则:1.新增功能,原则上不能修改被装饰函数源代码
   2.不能修改被函数的调用方式

实现装饰器的知识储备
1.函数即“变量” test(内存地址,函数名)即门牌号, def test():pass      test = ’函数体‘  ,calc = lambda x:x*3,没被del(没删除地址,只删除变量门牌号)的话就没被删除(执行顺序看函数调用方式顺序),定义占位和调用然后判断函数体所有有没有存在
def test(func):pass
def bar():pass
test(bar)相当于func=bar,把bar内存地址赋值给func,func()跟bar()一样的
2.高阶函数(注意函数名不带括号而不是函数值)
a.函数名作为实参传递作为另一个函数的变量(不改变被修饰函数源码情况下加上功能)
test(bar)(bar传给func地址传递)而不能是test(bar())(返回值传给变量)
b.返回的值包含函数名(不修改函数调用方式)
3.函数嵌套(函数体内又有def定义)

高阶函数+嵌套函数=》装饰器
#需修饰的函数不带参数:
import time


def wrapper(func):
    def decorate():
        start_time = time.time()
        func()
        end_time = time.time()
        print "the func run total time :%s" % (end_time-start_time)
        return 1
    return decorate


@wrapper   # 等同于bar= warpper(bar)哪里修饰,哪里加声明
def bar():
    time.sleep(3)
    print "in the bar"


bar()  #这里bar()与decorate()是一模一样的
print "--------------------------------"
print bar()   #先执行bar()后打印print bar()返回值   #打印返回值,会把decorate()函数执行一遍,然后返回值全部重新打印一下
结果:
D:\Python27\python.exe "D:/python-pycharm-installpackage/python script/our test/shoppingcart.py"
in the bar
the func run total time :3.0009×××752
--------------------------------
in the bar
the func run total time :3.0
1
---------------------------------------------------------------
#需修饰的函数带参数
# -*- coding: UTF-8 -*-
# __author__ = '10459'
import time


def wrapper(func):
    def decorate(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()
        print "the func run total time :%s" % (end_time-start_time)
        return 1
    return decorate


@wrapper   # 等同于bar= warpper(bar)
def bar():
    time.sleep(3)
    print "in the bar"


@wrapper
def bar1(name, age):
    time.sleep(5)
    print "hello,my name is %s,my ege is %d" % (name, age)

bar()
print "&&&&&&&&&&&&&&&&&&&&&&&&&&&"
bar1("Louis", 27)
结果:
D:\Python27\python.exe "D:/python-pycharm-installpackage/python script/our test/shoppingcart.py"
in the bar
the func run total time :3.0009×××752
&&&&&&&&&&&&&&&&&&&&&&&&&&&
hello,my name is Louis,my ege is 27
the func run total time :5.0

Process finished with exit code 0
------------------------------------------------
#不同地方访问带不同(取返回值的时候,函数执行先用一个变量接受它,然后print变量就可以,不然会重复执行函数,如return func()与return a,a=func()
# -*- coding: UTF-8 -*-
# __author__ = '10459'
import time

user,passwd = "Louis","qweqwe123"


def wrapper(auth_type):
    print "auth_type:", auth_type

    def in_wrapper(func):
        def decorate(*args, **kwargs):
            print "请输入用户名和密码:"
            username = raw_input("username:").strip()
            password = raw_input("password:").strip()
            if user == username and passwd == password:
                print "\033[32;1m user have passed authentication\033[0m"
                res = func(*args, **kwargs)
                print "\033[34;1m after authentication\033[0m"
                return res
            else:
                print "\033[41;1m invaild username or password \033[0m"
        return decorate
    return in_wrapper


@wrapper(auth_type="local")
def weibo():
    print "welcome to weibo page"


@wrapper(auth_type="local")     # qq = in_wrapper(qq)   in_wrapper=wrapper(auth_type="local")
def qq(name, age):
    print "hello,my name is %s,my ege is %d" % (name, age)
    return "from qq"


@wrapper(auth_type="lamp")
def weixin(name, sex):
    time.sleep(3)
    print "hello,my name is %s,my sex is %s" % (name, sex)

print "----开始用微博登陆----"
weibo()
print "----开始用qq登陆----"
a = qq("Louis", 27)
print a
print "----开始用微信登陆----"
weixin("Louis", "男")
结果:
D:\Python27\python.exe "D:/python-pycharm-installpackage/python script/our test/shoppingcart.py"
auth_type: local
auth_type: local
auth_type: lamp
----开始用微博登陆----
请输入用户名和密码:
username:Louis
password:qweqwe123
 user have passed authentication
welcome to weibo page
 after authentication
----开始用qq登陆----
请输入用户名和密码:
username:Louis
password:qweqwe123
 user have passed authentication
hello,my name is Louis,my ege is 27
 after authentication
from qq
----开始用微信登陆----
请输入用户名和密码:
username:Louis
password:qweqwe123
 user have passed authentication
hello,my name is Louis,my sex is 男
 after authentication

Process finished with exit code 0
-------------------------------------------------------
B.生成器
#函数的生成器
def fib(max):
    n, a, b = 0, 0, 1
    print "迭×××始"
    while n < max:
        yield b
        a, b = b, a+b    # =a+b,此时a还是之前的值,元组赋值是没有先后的注意
        n += 1
a = fib(6)
print type(a)   # 生成器  fib函数的地址,可以用for i in a来访问
print a.next()  #执行到yield b 然后暂停
print a.next()  #从yield b往下执行再到yield b
#表达式生成器

a = (i*2 for i in range(10))    # 这是一个生成器
# for i in a:
#     print i
b = []
for i in range(10):
    b.append(i*2)
# print b

print type(a)
while True:
    try:
        for i in range(12):
            print next(a)
    except StopIteration as e:
        print ("generator return value:", e)
        break
# 吃包子游戏(单线程当多线程用)
# -*- coding: UTF-8 -*-

import time


def customer(name):
    print "%s 开始吃包子拉" % name
    while True:
        baozi = yield
        print "\033[31;1m包子[%s]来了,被[%s]吃了\033[0m" % (baozi, name)
a = customer("Louis")
a.next()            # 仅唤醒生成器(第一次从开始到第yield,背后从yeild到下一次yield)
a.send("韭菜馅")     # 唤醒生成器(与next方法一个功能,只是多传值了),并传当前值给生成器
a.send("韭菜馅")
a.send("坑爹馅")


def producer(name):
    b = customer("张小增")
    b1 = customer("Louis")
    b2 = customer("木木")
    b.next()
    b1.next()
    b2.next()
    print "\033[41;1m%s开始做包子拉!!\033[0m" % name
    c = ("韭菜馅", "肉馅", "包菜馅", "芹菜馅", "粉丝馅", "糯米馅")
    for i in c:
        time.sleep(2)
        print ("做了一个包子,分成三份")
        b.send(i)
        b1.send(i)
        b2.send(i)
    return type(c)
producer("nick")

结果:
D:\Python27\python.exe "D:/python-pycharm-installpackage/python script/our test/shoppingcart.py"
Louis 开始吃包子拉
包子[韭菜馅]来了,被[Louis]吃了
包子[韭菜馅]来了,被[Louis]吃了
包子[坑爹馅]来了,被[Louis]吃了
张小增 开始吃包子拉
Louis 开始吃包子拉
木木 开始吃包子拉
nick开始做包子拉!!
做了一个包子,分成三份
包子[韭菜馅]来了,被[张小增]吃了
包子[韭菜馅]来了,被[Louis]吃了
包子[韭菜馅]来了,被[木木]吃了
做了一个包子,分成三份
包子[肉馅]来了,被[张小增]吃了
包子[肉馅]来了,被[Louis]吃了
包子[肉馅]来了,被[木木]吃了
做了一个包子,分成三份
包子[包菜馅]来了,被[张小增]吃了
包子[包菜馅]来了,被[Louis]吃了
包子[包菜馅]来了,被[木木]吃了
做了一个包子,分成三份
包子[芹菜馅]来了,被[张小增]吃了
包子[芹菜馅]来了,被[Louis]吃了
包子[芹菜馅]来了,被[木木]吃了
做了一个包子,分成三份
包子[粉丝馅]来了,被[张小增]吃了
包子[粉丝馅]来了,被[Louis]吃了
包子[粉丝馅]来了,被[木木]吃了
做了一个包子,分成三份
包子[糯米馅]来了,被[张小增]吃了
包子[糯米馅]来了,被[Louis]吃了
包子[糯米馅]来了,被[木木]吃了
--------------------------------------------------------------