在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
我知道星号用于解包系统参数等值或将列表解包到变量中。
但我之前在这个 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