• Python学习基础(三)——装饰器,列表生成器,斐波那契数列


    装饰器——闭包

    # 装饰器  闭包
    
    '''
    
        如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包
    
        闭包 = 函数块  +  定义时的函数环境
    
    '''
    
    def f():
        x = 100
        y = 200
    
        def mytext():
            return x + y
    
        return mytext
    
    
    s=f()
    print(s())

    装饰器——高潮1

    import time
    
    
    def foo():
        print("foo..........")
        time.sleep(2)
    
    
    def root():
        print("root---------")
        time.sleep(1)
    
    
    def get_time(h):
        start = time.time()
        h
        stop = time.time()
        print("运行时间为: %s" % (stop - start))
    
    
    get_time(root())

    装饰器——高潮2

    import time
    
    def get_time(h):
        def inner():
            start = time.time()
            h()
            stop = time.time()
            print("运行时间为: %s" % (stop - start))
    
        return inner
    
    
    @get_time
    def foo():
        print("foo..........")
        time.sleep(2)
    
    
    @get_time
    def root():
        print("root---------")
        time.sleep(1)
    
    
    foo()

    装饰器——函数功能添加参数

    import time
    
    
    def get_time(f):
        def inner(*s):
            start = time.time()
            f(*s)
            end = time.time()
            print("使用时长为: %s" % (end - start))
    
        return inner
    
    
    @get_time
    def add(*a):
        sum = 0
        for i in a:
            sum += i;
        print(sum)
        time.sleep(2)
    
    
    add(2, 3, 5, 7)

    列表生成式

    # 列表生成式
    a = [x * x for x in range(10)]
    print(a)

    列表生成器

    # 列表生成器
    
    s = (x * 2 for x in range(5))
    # print(s)
    
    # print(s.__next__())
    print(next(s))
    
    print(next(s))
    print(next(s))
    print(next(s))
    print(next(s))
    
    print("*-*--**--*--*-*-*-*---*")
    
    
    def foo():
        print("ok1")
        yield 1
        print("ok2")
        yield 2
    
    
    g = foo()
    # next(g)
    # next(g)
    for i in g:
        # while True:
        #     i = next(foo())
        print(i)
    print("/*//***/*/***//*/**/*")
    
    a = [1, 2, 3]

    斐波那契数列

    def fib(m):
        i, a, b = 0, 0, 1
        while i < m:
            print(b)
            a, b = b, a + b
            i += 1
    
    
    # fib(10)
    
    
    z = 10
    c = 20
    
    print("------------")
    
    z, c = c, z + c
    
    print(z)
    print(c)
    
    print("*************")
    
    z = 10
    c = 20
    
    z = c
    c = z + c
    
    print(z)
    print(c)
    
    print("-------------")
    【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处!
    【重要说明】本文为本菜鸟的学习记录,论点和观点仅代表个人不代表此技术的真理,目的是学习和可能成为向别人分享的经验,因此有错误会虚心接受改正,但不代表此时博文无误!
    【博客园地址】JayveeWong: http://www.cnblogs.com/wjw1014
    【CSDN地址】JayveeWong: https://blog.csdn.net/weixin_42776111
    【Gitee地址】Jayvee:https://gitee.com/wjw1014
    【GitHub地址】Jayvee:https://github.com/wjw1014
  • 相关阅读:
    Codeforces Round #313 (Div. 1) A.Gerald's Hexagon
    COJN 0585 800604鸡蛋的硬度
    COJN 0584 800603吃糖果
    COJN 0583 800602分苹果
    COJN 0575 800601滑雪
    昨天的补记
    重构的代码
    写了一个复杂的sql语句
    一个想法
    安装了C
  • 原文地址:https://www.cnblogs.com/wjw1014/p/8656818.html
Copyright © 2020-2023  润新知