我认为这被称为"位置参数"或相反或关键字参数。我在Python Django和rest框架中编写了一个脚本,它必须从用户那里获取参数并将其提供给Amazon API。这是代码;
page = request.query_params.get('page');
search = request.query_params.get('search');
search_field = request.query_params.get('search_field');
search_index = request.query_params.get('search_index');
response = amazon.ItemSearch(Keywords=search, SearchIndex=search_index, ResponseGroup="ItemAttributes,Offers,Images,EditorialReview,Reviews")
在此代码中,文字Keywords=search
会有所不同。那是。它可能像Actor = search,Title = search或Publisher = search我不知道如何使Keywords
部分动态,以便它改变为用户的输入,如Actor,Title,或出版商
答案 0 :(得分:1)
我认为您应该构建一个kwargs字典,然后使用splice:"-1"
语法扩展它以进行函数调用。我将举一个假设**
查询参数更改搜索的示例:
search_field
答案 1 :(得分:1)
我使用了字典。我就是这样做的;
page = request.query_params.get('page');
search = request.query_params.get('search');
search_field = request.query_params.get('search_field');
search_index = request.query_params.get('search_index');
dict = {'SearchIndex':search_index, 'ResponseGroup': 'ItemAttributes,Offers,Images,EditorialReview,Reviews'}
dict[search_field] = search;
response = amazon.ItemSearch(**dict)
我刚用动态变量作为键传递了字典。