蟒蛇 3.8.6
有我的自定义文字:
from typing import Literal, get_args
_ACTIONS = Literal["pasteCharAt", "copyCharAt", "deleteCharAt", "replaceCharAt",
"selectAll", "deleteAll", "copyAll", "pasteAllAt", "click",
"moveLeft", "moveRight"]
如何生成它们的随机选择? 对于
print(f'random.choice(_ACTIONS) = {random.choice(_ACTIONS)}')
只得到:
File "C:\python38\lib\random.py", line 288, in choice
i = self._randbelow(len(seq))
TypeError: object of type '_GenericAlias' has no len()
或为:
print(f'random.choice(list(_ACTIONS)) = {random.choice(list(_ACTIONS))}')
得到:
File "C:\python38\lib\typing.py", line 261, in inner
return func(*args, **kwds)
File "C:\python38\lib\typing.py", line 685, in __getitem__
params = tuple(_type_check(p, msg) for p in params)
File "C:\python38\lib\typing.py", line 685, in <genexpr>
params = tuple(_type_check(p, msg) for p in params)
File "C:\python38\lib\typing.py", line 149, in _type_check
raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Parameters to generic types must be types. Got 0.