相关疑难解决方法(0)

2149
推荐指数
17
解决办法
65万
查看次数

理解Python中的星号运算符位于括号中的函数之前

我知道星号用于解包系统参数等值或将列表解包到变量中。

但我之前在这个 asyncio 示例中没有见过这种语法。

我在这里阅读这篇文章,https://realpython.com/async-io-python/#the-10000-foot-view-of-async-io,但我不明白星号运算符在这做什么语境。

#!/usr/bin/env python3
# rand.py

import asyncio
import random

# ANSI colors
c = (
    "\033[0m",   # End of color
    "\033[36m",  # Cyan
    "\033[91m",  # Red
    "\033[35m",  # Magenta
)

async def makerandom(idx: int, threshold: int = 6) -> int:
    print(c[idx + 1] + f"Initiated makerandom({idx}).")
    i = random.randint(0, 10)
    while i <= threshold:
        print(c[idx + 1] + f"makerandom({idx}) == {i} too low; retrying.")
        await asyncio.sleep(idx + 1)
        i = random.randint(0, 10)
    print(c[idx + …
Run Code Online (Sandbox Code Playgroud)

python syntax asynchronous argument-unpacking python-asyncio

4
推荐指数
1
解决办法
1532
查看次数