Posts

    Python高级语法

    pre

    Read The Fucking Source Code.

    A picture is worth a thousand words

    MISC

    asterisk (*)

    基础

    传参

    *args表示*args enable us to pass the variable number of non-keyword arguments to functions, but we cannot use this to pass keyword arguments. Keyword arguments mean that they contain a key-value pair, like a Python dictionary.

    **kwargsallows us to pass any number of keyword arguments.

    修饰器

    举个例子:

    def hello_decorator(func):
        def inner1(*args, **kwargs):
    
            print("before Execution")
    
            # getting the returned value
            returned_value = func(*args, **kwargs)
            print("after Execution")
    
            # returning the value to the original frame
            return returned_value
    
        return inner1
    
    
    # adding decorator to the function
    @hello_decorator
    def sum_two_numbers(a, b):
        print("Inside the function")
        return a + b
    
    a, b = 1, 2
    
    # getting the value through return of the function
    print("Sum =", sum_two_numbers(a, b))
    

    Output:

    before Execution
    Inside the function
    after Execution
    Sum = 3
    

    chain decorators

    # code for testing decorator chaining 
    def decor1(func): 
        def inner(): 
            x = func() 
            return x * x 
        return inner 
    
    def decor(func): 
        def inner(): 
            x = func() 
            return 2 * x 
        return inner 
    
    @decor1
    @decor
    def num(): 
        return 10
    
    @decor
    @decor1
    def num2():
        return 10
    
    print(num()) 
    print(num2())
    

    Output:

    400
    200
    

    协程

    一系列api来执行并行任务,给出一些区别。

    协程

    import asyncio
    
    async def main():
        print('hello')
        await asyncio.sleep(1)
        print('world')
    
    asyncio.run(main())
    
    hello
    world
    

    注意:简单地调用一个协程并不会使其被调度执行

    main()
    <coroutine object main at 0x1053bb7c8>
    

    可等待对象

    可等待 对象有三种主要类型: 协程, 任务 和 Future.

    协程
    import asyncio
    
    async def nested():
        return 42
    
    async def main():
        # Nothing happens if we just call "nested()".
        # A coroutine object is created but not awaited,
        # so it *won't run at all*.
        nested()
    
        # Let's do it differently now and await it:
        print(await nested())  # will print "42".
    
    asyncio.run(main())
    
    任务
    import asyncio
    
    async def nested():
        return 42
    
    async def main():
        # Schedule nested() to run soon concurrently
        # with "main()".
        task = asyncio.create_task(nested())
    
        # "task" can now be used to cancel "nested()", or
        # can simply be awaited to wait until it is complete:
        await task
    
    asyncio.run(main())
    
    Futures
    import asyncio
    async def main():
        await function_that_returns_a_future_object()
    
        # this is also valid:
        await asyncio.gather(
            function_that_returns_a_future_object(),
            some_python_coroutine()
        )
    

    一个很好的返回对象的低层级函数的示例是 loop.run_in_executor()。

    创建 任务

    任务取消

    任务组¶

    休眠¶

    并发运行任务¶

    主动任务工厂¶

    屏蔽取消操作¶

    超时

    简单等待

    在线程中运行¶

    跨线程调度¶

    内省¶

    Task 对象¶