**kwargs vs 10 arguments in a python function?
Asked Answered
H

3

5

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?

Hardtop answered 3/6, 2011 at 6:5 Comment(6)
What fields do other functions need to be able to change?Mccully
didn't get that. all of these fields are only local to this function.Hardtop
In other words, what fields does the program need to specify on the fly?Mccully
Well, all the fiends in the string request should be passed by the user of the method. The only diff is, all the fields are optional except <keywords>ipod</keywords>, which is mandatory.Hardtop
I think this is a case of "explicit is better"Budge
@gnibbler so, in that case: how should I construct the string? check and add?Hardtop
L
7

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.

Loats answered 3/6, 2011 at 6:21 Comment(2)
yeah, I am also in favor of this approach, i.e. approach 2.Hardtop
Agree. **kwargs is primarily useful when you don't know ahead-of-time what argument names you're expecting (see, eg, str.format). If you're expecting particular arguments, it is better to specify them explicitly.Psalmody
T
0

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()
Transfiguration answered 3/6, 2011 at 6:12 Comment(4)
I would like to know too, this looks like good to me. But I am not aware about the pythonic ways.Hardtop
Presumably, you meant req = FindBestMatchItemDetailsAcrossStoresRequest('ipod')?Punchboard
updated my question. I think you misunderstood my question. In this case, what if the user wants to pass: siteResultsPerPage? how will I achieve that?Hardtop
@Johnsyweb It would throw TypeError, as Gareth forgot the self's. Also, i don't see the point of using a class over, say, a defaultdict.Loats
M
0

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)

Marinna answered 3/6, 2011 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.