[Python-ideas] Improving fn(arg=arg, name=name, wibble=wibble) code

Anders Hovmöller boxed at killingar.net
Fri Sep 14 03:17:09 EDT 2018


> it reads fine precisely because the variables names match with the signature of new_file(). But if new_file() is changed they won't match up anymore and it will still read fine and look ok, but now the parameters don't line up and it's broken in potentially very subtle ways. For example if content_type and file_name switch places. Those are the same time but the consequences for the mixup are fairly large and potentially very annoying to track down. 
> 
> Do you disagree on this point?
> 
> The mixup you describe would probably cause an immediate error as the filename would not be a valid content type. I suspect it'd be easy to track down.

So no you don't agree?

> Keyword arguments are especially important when the type and value of two arguments are hard to distinguish, like a height and width. If one is an int and the other is a str, or if it must be a str with only a handful of valid values, I'm not so worried about making mistakes.
> 
> Further, I think it's reasonable to use keyword arguments [...]

...now I'm confused. Now it sounds like you do agree? 

> [...] here and no more "painful" than the use of positional arguments in this case. Both are annoyingly verbose.

That's a bit of a dodge. There is a huge difference in verbosity between 

handler.new_file(field_name, file_name, content_type, content_length, charset, content_type_extra)

and 

handler.new_file(field_name=field_name, file_name=file_name, content_type=content_type, content_length=content_length, charset=charset, content_type_extra=content_type_extra)

and it's pretty obvious when it's spelled out. Now compare to my two suggested syntaxes:

handler.new_file(*, field_name, file_name, content_type, content_length, charset, content_type_extra)
handler.new_file(=field_name, =file_name, =content_type, =content_length, =charset, =content_type_extra)

>  I'd prefer refactoring the new_file method, but I didn't spot where it's defined.

People keep saying that, but I don't buy it. Refactoring isn't magic, it won't just make the data required to a function go away. 

I've been pressed to get hard numbers, I have. I've been pressed to come up with actual examples, I have. Now when I have a point you're just waving your hand and saying "refactoring". I don't think that's an actual argument.

/ Anders
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180914/10cfa7b4/attachment.html>


More information about the Python-ideas mailing list