• 函数进阶(一)


    闭包函数

     1.闭包函数必须在函数内部定义
     2.闭包函数可以引用外层函数的名字
     闭包函数是 函数嵌套、函数对象、名称空间与作用域 结合体。

    直接调用

    # def func(x):
    #     print(x)
    # func(1010)

    通过闭包函数传参

    # def outer(number):
    #     print(number)
    #     def inner():
    #         print(number)
    #     return inner
    #
    # func = outer(100)
    # func()

    直接传入地址,爬取小哥哥的博客

    # import requests
    # def spider_func(url):
    #     response = requests.get(url)
    #     if response.status_code == 200:
    #         print(len(response.text))
    #         print(response.text)
    # url = 'https://www.cnblogs.com/zhulipeng-1998/'
    # spider_func(url)

    通过闭包传入地址,爬取小哥哥博客

    # import time
    # import requests
    # def spider_outer(url):
    #     def spider_inner():
    #         start_time = time.time()
    #         response = requests.get(url)
    #         end_time = time.time()
    #         if response.status_code == 200:
    #             print(f'爬取需要的时间{end_time - start_time}')
    #             print(len(response.text))
    #             print(response.text)
    #     return spider_inner
    # spider_jingdong = spider_outer('https://www.cnblogs.com/zhulipeng-1998/')
    # spider_jingdong()

    试着让用户输入网址爬出内容,没有成功

    
    import time
    import requests
    def spider_outer(url):
        def spider_inner():
            start_time = time.time()
            response = requests.get(url)
            end_time = time.time()
            if response.status_code == 200:
                print(f'爬取需要的时间{end_time - start_time}')
                print(len(response.text))
                print(response.text)
        return spider_inner
    chioce = input('请输入爬取网址:')
    spider_chioce = spider_outer('chioce')
    spider_chioce()

    装饰器:

    不修改被装饰对象的源代码
    不修改被装饰对象的调用方式

    闭包传入,添加计时功能

    # import time
    #
    # def time_record(func):
    #     start_time = time.time()
    #     func()
    #     end_time = time.time()
    #     print(f'下载电影消耗的时间{end_time - start_time}')
    #     return '苍天老师的大片'
    # def download_movie():
    #     print('开始下载电影....')
    #     time.sleep(5)
    #     print('电影下载成功...')
    #     return
    # res = time_record(download_movie)
    # for i in res:
    #     print(i)

    初级装饰器,无返回值

    # import time
    # def download_movie(name):
    #     print('开始下载电影.....')
    #     time.sleep(3)
    #     print('电影下载成功.....')
    #     return f'{name}救援队高清无码.mp4'
    #
    # def time_record(func):
    #     def inner(*args, **kwargs):
    #         start_time = time.time()
    #         res = func(*args, **kwargs)
    #         end_time = time.time()
    #         print(f'下载消耗时间{end_time - start_time}')
    #         return res
    #     return inner
    # download_movie = time_record(download_movie)
    # download_movie(name='nick')

    有返回值,但是没有接收到,打印了res,不知道对不对

    # import time
    # def download_movie():
    #     print('开始下载电影.....')
    #     time.sleep(3)
    #     print('电影下载成功.....')
    #     return '救援队高清无码.mp4'
    #
    # def time_record(func):
    #     def inner(*args, **kwargs):
    #         start_time = time.time()
    #         res = func(*args, **kwargs)
    #         print(res)
    #         end_time = time.time()
    #         print(f'下载消耗时间{end_time - start_time}')
    #         return res
    #     return inner
    #
    # download_movie = time_record(download_movie)
    # download_movie()

    最终版本

    # def wrapper(func):
    #     def inner(*args,**kwargs):
    #         username = input('请输入名字')
    #         if username == 'dsb':
    #             res = func(*args,**kwargs)
    #             return res
    #
    #         else:
    #             print('用户权限不足')
    #     return inner
    #
    # def func1():
    #     pass
    #
    # func1 = wrapper(func1)
    # func1()
    
    
    ### def wrapper(func):
            def inner(*args,**kwargs):
                res = func(*args,**kwargs)
                return res
            return inner

    魔法糖

    import time
    '''def wrapper(func):
        def inner(*args,**kwargs):
            res = func(*args, **kwargs)
            return res
        return inner
    
    @wrapper
    def func1():
        pass
    def func2():
        pass
    
    func1()'''
    我把月亮戳到天上 天就是我的 我把脚踩入地里 地就是我的 我亲吻你 你就是我的
  • 相关阅读:
    python--函数的返回值、函数的参数
    python--字典,解包
    Vue--ElementUI实现头部组件和左侧组件效果
    Vue--整体页面布局
    jmeter--non GUI
    python--切片,字符串操作
    celery--调用异步任务的三种方法和task参数
    celery--实现异步任务
    celery--介绍
    开发问题记录
  • 原文地址:https://www.cnblogs.com/zhulipeng-1998/p/12863918.html
Copyright © 2020-2023  润新知