**kwargs 与 python 函数中的 10 个参数?

发布于 2024-11-11 17:31:43 字数 1542 浏览 4 评论 0原文

我从 python 开始,尝试为 ebay 网络服务

现在,我的问题是:

说,这是我的功能:

def findBestMatchItemDetailsAcrossStores():
     request = """<?xml version="1.0" encoding="utf-8"?>
     <findBestMatchItemDetailsAcrossStoresRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
     <siteResultsPerPage>50</siteResultsPerPage>
     <entriesPerPage>50</entriesPerPage>
     <ignoreFeatured>true</ignoreFeatured>
     <keywords>ipod</keywords> <-----REQUIRED
     <itemFilter>
     <paramName>PriceMin</paramName>
     <paramValue>50</paramValue>
     <name>Currency</name>
     <value>USD</value>
     </itemFilter>
     <itemFilter>
     <paramName>PriceMax</paramName>
     <paramValue>100</paramValue>
     </itemFilter>
     </findBestMatchItemDetailsAcrossStoresRequest>"""
     return get_response(findBestMatchItemDetailsAcrossStores.__name__, request)

其中,关键字是唯一必填字段。那么,我应该如何构造该方法呢?方法可以是:

  1. 创建一个对象,将其传递给 func(object) :java 方式
  2. 传递所有参数: func(a=val1, b=val2, c=val3, d=val4 etc)
  3. 使用 **kwargs 和相信调用该函数的人,他会传递正确的键和值,因为我将使用这些键来实际构造 XML 标记。

更新:

您在请求中看到的所有 xml 标记都需要由用户传递。但是关键字应该被传递,并且如果需要也可以传递其他关键字。

有什么建议吗?

I am starting out with python and trying to construct an XML request for an ebay web service:

Now, my question is:

Say, this is my function:

def findBestMatchItemDetailsAcrossStores():
     request = """<?xml version="1.0" encoding="utf-8"?>
     <findBestMatchItemDetailsAcrossStoresRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
     <siteResultsPerPage>50</siteResultsPerPage>
     <entriesPerPage>50</entriesPerPage>
     <ignoreFeatured>true</ignoreFeatured>
     <keywords>ipod</keywords> <-----REQUIRED
     <itemFilter>
     <paramName>PriceMin</paramName>
     <paramValue>50</paramValue>
     <name>Currency</name>
     <value>USD</value>
     </itemFilter>
     <itemFilter>
     <paramName>PriceMax</paramName>
     <paramValue>100</paramValue>
     </itemFilter>
     </findBestMatchItemDetailsAcrossStoresRequest>"""
     return get_response(findBestMatchItemDetailsAcrossStores.__name__, request)

Where, keyword is the only required field. So, how should I construct the method? The ways can be:

  1. Create an object, pass it to the func(object) : The java way
  2. Pass all the arguments: func(a=val1, b=val2, c=val3, d=val4 etc)
  3. Use **kwargs and trust the person who calls the function, that he passes the right keys with the values, because I will use the keys to actually construct the XML tags.

Update:

All the xml tags you see in the request are needed to be passed by the user. But keywords should be passed and others maybe passed if required.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倥絔 2024-11-18 17:31:43

一个好主意是将所有具有适当默认值(或仅 None 默认值)的参数放入函数签名中。是的,它需要在函数本身中进行更多的输入,但是界面将是干净的、自记录的并且易于使用,因为您不必在 eBay 文档或函数源中查找可能的参数。这将节省您以后的时间。

A good idea is to put all the parameters with appropriate defaults (or just None defaults) in the function signature. Yeah, it will require a little more typing in the function itself, but the interface will be clean, self-documented and simple to use, as you won't have to look up possible parameters in ebay docs or function source. It will save you time later on.

只等公子 2024-11-18 17:31:43

将消息建模为一个类怎么样?

class FindBestMatchItemDetailsAcrossStoresRequest:
     def __init__(self,keywords):
         self.keywords = keywords # required parameters in the constructor
         # set up the default values....etc
         self.siteResultsPerPage = 50 
         self.name = 'Currency'


     def send(self):
         # build message from self.xxx
         return get_response()


 #usage
 req = FindBestMatchItemDetailsAcrossStoresRequest('ipod')
 response = req.send()

 #usage with optional args

 req.siteResultsPerPage = 150
 response = req.send()

how about modeling the message as a class?

class FindBestMatchItemDetailsAcrossStoresRequest:
     def __init__(self,keywords):
         self.keywords = keywords # required parameters in the constructor
         # set up the default values....etc
         self.siteResultsPerPage = 50 
         self.name = 'Currency'


     def send(self):
         # build message from self.xxx
         return get_response()


 #usage
 req = FindBestMatchItemDetailsAcrossStoresRequest('ipod')
 response = req.send()

 #usage with optional args

 req.siteResultsPerPage = 150
 response = req.send()
浅浅 2024-11-18 17:31:43

我会为所有参数使用命名参数。通过这样做,可以很容易地分配默认值,并强制用户提供所需的参数(通过省略默认值)

I would use named parameters for all. By doing this it is very easy to assign default values, and to force the user to supply required parameters (by omitting the default)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文