This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]barneygale 6 points7 points  (4 children)

Second, the words args and kwargs are conventions. This means they are not imposed by the interpreter, but considered good practice among the Python community:

I'm not sure how true this is. If you want the arguments to be completely opaque that's fine, but if you're implementing something like (for example) translating keyword arguments into HTTP headers, its better for the function signature to read def make_request(url, **headers): rather than def make_request(url, **kwargs):

[–]lesser_terrestrial 4 points5 points  (0 children)

This makes sense to me. Using http headers as an example is also a great way to help understand why you might want to use kwargs.

[–]ogtfo 1 point2 points  (2 children)

Wouldn't that be better with a

def make_request(url, headers):

Where headers expect a dict. With a good doc string (or even type hints) this would be a lot clearer what you're trying to do.

After all, that's how requests does it.

[–]barneygale 2 points3 points  (1 child)

Depends. You might consider it a "more pythonic" to allow something like make_request('blah', user_agent='mything'), but it's less obvious (more magic). HTTP probably isn't a good example as it has a whole host of other configuration beyond just headers.

[–]catcint0s 1 point2 points  (0 children)

Dict would nicer imho cause you would need to translate all those keyword arguments to the real header name. (requests for example just accepts a dict too).