tun*_*ung 2 python types return typing python-typing
我有一个函数将返回一个字典,例如:
from typing import Dict
def foo() -> Dict[]:
params = {
"side": "Buy",
"symbol": "BTCUSDT",
"order_type": "Market",
"time_in_force": "PostOnly",
"reduce_only": False,
"close_on_trigger": False,
}
return {
"method": "POST",
"api": "private/linear/order/create",
"body": params
}
Run Code Online (Sandbox Code Playgroud)
里面的内容应该是什么Dict[]?
如果你的字典的键都是字符串并且是固定的,那么TypedDictPython3.8+中的 在这里非常合适:
from typing import TypedDict
class Params(TypedDict):
side: str
symbol: str
order_type: str
time_in_force: str
reduce_only: bool
close_on_trigger: bool
class Foo(TypedDict):
method: str
api: str
body: Params
def foo() -> Foo:
params: Params = {
"side": "Buy",
"symbol": "BTCUSDT",
"order_type": "Market",
"time_in_force": "PostOnly",
"reduce_only": False,
"close_on_trigger": False,
}
return {
"method": "POST",
"api": "private/linear/order/create",
"body": params
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1683 次 |
| 最近记录: |