【发布时间】:2018-02-27 03:38:20
【问题描述】:
我目前正在开发 API Wrapper,但在将参数从函数传递到请求的负载时遇到了问题。参数可以是blockId、senderId、recipientId、limit、offset、orderBy。所有参数由“或”连接。一种可能的解决方案可能是对每种组合都有 if 语句,但我认为这是一种糟糕的方法。 (请求和常量已经导入)
def transactionsList(*args **kwargs):
if blockId not None:
payload = {'blockId': blockId}
if offset not None:
payload = {'offset': offset}
...
r = requests.get(constants.TRANSACTIONS_LIST, params=payload, timeout=constants.TIMEOUT)
return r
什么是(或者是)更优雅的方法来实现传递给请求有效负载的函数参数?
【问题讨论】:
标签: python parameters python-requests parameter-passing