zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Java Web----Java Web的数据库操作(二)
    PHP foreach 遍历数组是打印出相同的数据
    implemented loader.php
    With PHP frameworks, why is the “route” concept used?
    网络包
    长寿之道
    php中print_r 和var_dump 打印变量的区别。
    php 中的全局变量的理解
    php session 严格过期时间实现
    nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况
  • 原文地址:https://www.cnblogs.com/wjw1014/p/8656818.html
Copyright © 2011-2022 走看看