zoukankan      html  css  js  c++  java
  • Python学习-day4

    学习装饰器,首先听haifeng老师讲解了一下准备知识。

    1.函数即变量

    2.高阶函数+嵌套函数==》装饰器

     装饰器的作用是在,1)不改变源代码,2)不改变原函数的调用方式的前提下为函数增加新的功能。

    首先学习了最基本的装饰器

    使用方法:在原先函数定义的上方加入@wrapper语法糖,等价于 func = wrapper(func)

    如果原函数带参数,则在第二层函数加入(*args,**kwargs

    如果装饰器也需要带参数,则需要再次嵌套一层,并且参数传递的顺序为:

    def deco(args):

      def first(args):

        def second(args):

        return second

      return first

    @deco(args)    #args --> 装饰器第一层

    def func(args):    #func --> 装饰器第二层  args --> 装饰器第三层
      pass

    #Authon Ivor
    import time
    
    #装饰器
    def timer(func):
        def deco(*args,**kwargs):
            start_time = time.time()
            func(*args,**kwargs)
            stop_time = time.time()
            print("The program cost %s" % (stop_time-start_time))
        return deco
    #装饰方法 @timer # 等价于 test1 = timer(test1) def test1(): time.sleep(1.5) print("I'm in the test1 !")
    @timer
    def test2(name,age): time.sleep(0.5) print("%s's age is %s" % (name,age)) test1() test2("Ivor",25)


    下面是武sir的博客,是高阶装饰器的一个小例子

    #!/usr/bin/env python
    
    #coding:utf-8
    
      
    
    def Before(request,kargs):
    
        print 'before'
    
          
    
    def After(request,kargs):
    
        print 'after'
    
      
    
      
    
    def Filter(before_func,after_func):
    
        def outer(main_func):
    
            def wrapper(request,kargs):
    
                  
    
                before_result = before_func(request,kargs)
    
                if(before_result != None):
    
                    return before_result;
    
                  
    
                main_result = main_func(request,kargs)
    
                if(main_result != None):
    
                    return main_result;
    
                  
    
                after_result = after_func(request,kargs)
    
                if(after_result != None):
    
                    return after_result;
    
                  
    
            return wrapper
    
        return outer
    
          
    
    @Filter(Before, After)
    
    def Index(request,kargs):
    
        print 'index'

    列表生成器、可迭代对象、迭代器

    #列表生成器
    #[i for i in range(100)]
    
    #生成器
    #(i for i in range(100))
    #生成器只有在调用的时候才会生成相应的数据
    #只记录当前的位置
    #只有一个__next__()方法
    
    #可迭代对象
    #from collections import Iterable
    #isinstance([],Iterable)
    #列表,字典,元组等都是可迭代对象
    
    #迭代器
    #有next方法的才是迭代器
    
    #能用for循环的都是可迭代对象
    #能用next方法的都是迭代器对象


    协程,简单消费者模型

    #Authon Ivor
    import time
    
    def consumer(name):
        print("我准备吃包子啦!")
        while True:
            baozi = yield
            print("包子(%s)来了,(%s)吃掉了!" % (baozi,name))
    
    
    def producer():
        c1 = consumer("A")
        c2 = consumer("B")
        c1.__next__()
        c2.__next__()
        print("我开始做包子了!")
        for i in range(10):
            print("做好了一个!")
            time.sleep(1)
            c1.send(i)
            c2.send(i)
    
    producer()

    斐波那契数列

    #Authon Ivor
    
    def fib(max):
        n,a,b=0,0,1
        for n in range(max):
            yield b
            a,b=b,a+b
        return "done"
    
    f = fib(15)
    for i in f:
        print(i)
    View Code
  • 相关阅读:
    webpack的安装与配置
    npm初始化
    gitignore的配置
    git本地已有文件夹和远程仓库对应
    git 配置
    开发环境和开发工具
    git 码云使用教程
    递归
    LeetCode 392. 判断子序列
    MongoDB基本操作
  • 原文地址:https://www.cnblogs.com/Darksugar/p/6382092.html
Copyright © 2011-2022 走看看