python unwrapped语句
时间: 2023-07-06 12:30:00 浏览: 116
`Unwrapped`是Python中的一个装饰器函数,它的作用是将被装饰函数返回值中的`Future`对象进行解包,即将其中的结果提取出来。`Future`是Python中的一个异步编程概念,表示一个尚未完成的操作,可以通过`await`关键字来等待它的结果。
使用`@unwrapped`装饰器可以使得被装饰函数的返回值中的`Future`对象自动解包,从而简化异步编程的代码。例如:
```python
import asyncio
@asyncio.coroutine
def coro():
return asyncio.Future()
@unwrapped
@asyncio.coroutine
def wrapped_coro():
return asyncio.Future()
async def main():
fut1 = coro()
fut2 = wrapped_coro()
print(fut1) # <Future pending>
print(fut2) # <Future pending> (unwrapped)
asyncio.run(main())
```
在上面的例子中,`coro`函数返回了一个`Future`对象,而`wrapped_coro`函数在其定义时使用了`@unwrapped`装饰器。在`main`函数中,我们分别调用这两个函数,并将它们返回的`Future`对象赋值给`fut1`和`fut2`变量。由于`wrapped_coro`函数使用了`@unwrapped`装饰器,因此在返回`Future`对象时,该对象会被自动解包,即返回其中的结果。所以`fut2`变量的值是`<Future pending> (unwrapped)`,而`fut1`变量的值仍然是`<Future pending>`。
阅读全文
相关推荐

















