[Python-ideas] About the passing the function arguments in Keyword form.

Anders Hovmöller boxed at killingar.net
Mon Dec 24 07:05:17 EST 2018



> On 24 Dec 2018, at 11:21, 李默 <phylimo at 163.com> wrote:
> 
> I am having an idea on loosing the argument validity check when passing the function arguments in keyword way. 
> For example:
> -------------------------------
> def f(x, y):
>     print(x, y)
> def call_f():
>     f(x=7, y=9, z=9)
> 
> call_f()
> ------------------------------
> In the current of python, the extra pass of 'z' would let the interpreter raise an exception and stop work.  My idea is that the interpreter need not stop because all the needed args are completely provided.  Of course for this toy example, 'f' can be define as  f(x, y, **kwargs) to achieve the same goal.  However,  essentially it is reasonably to keep interpreter going as long as enough args are passed.  And this modification can bring more freedom of programming. 

Similar features exists in JavaScript (where you can also do the same thing with positional arguments), and Clojure to make two. 

I personally think this is extremely bad. This type of behavior can make error in your code slip by undetected for a very long time. Let's take a concrete example! We have a function:

def foo(*, a, b=3, c):
    ....

People call it like so:

foo(a=7, b=1, c=11)

Now what happens if we rename argument b to q? The above code still runs! It just now passes 3 (the default value) to foo instead of the intended 1. 

I hope this example is enough to convince you of the danger of such a feature. It's certainly the reason why I think JavaScript and Clojure are terrible when it comes to passing arguments :)

Best regards
Anders
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181224/4d6fde01/attachment.html>


More information about the Python-ideas mailing list