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

Robert Vanden Eynde robertve92 at gmail.com
Tue Dec 25 05:03:36 EST 2018


It's very important that f(z=5) Raises an exception if z is not an argument.

For your case, I'd do a wrapper, instead lf calling f(z=5) you can call
UniversalCall(f, x=1, y=2, z=5) if you want to specify it on the caller
side.

Or else, you can create a decorator :

@universal_callable
def f(x, y):
    ...

f(x=1, y=2, z=5)  # works !

On Mon, 24 Dec 2018, 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.
>
>
> Think about the following situations:
>
> situation 1) there are many 'f's written by other people, and their args are very similar and your job is to run each of them to get some results.
>
> ---------------------
>
> ##########code by others:
>
> def f0():
> 	...
> def f1(x):
> 	...
> def f2(x, y):
> 	...
> def f3(x, y, z):
> 	...
>
> #if passing extra args are valid, you can run all the functions in the following way, which is very compact and easy to read.
>
> def test_universal_call():
>
> 	funcs = [f0, f1, f2, f3]
> 	args = {'x':1, 'y':5, 'z':8}
> 	for f in funcs:
> 		f(**args)
>
> ------------------
>
>
> situation 2) there are several steps for make one product, each step is in an individual function and needs different args.
>
> ------------------
>
> def make_oil(oil):
> 	...
>
> def make_water( water):
> 	...
>
> def make_powder(powder):
> 	...
>
> ## if passing extra args are valid, you can run all the functions in the following way, which is very compact and easy to read.
>
> def dish():
> 	procedures = [make_oil, make_water, make_powder]
>
> 	args = {'oil' : 1, 'water': 10, 'powder': 4}
> 	for f in procedures:
> 		f(**args)
>
>
> ---------------
>
>
> This idea is different from **kwargs. **kwargs are used when user wants to record all the keywords passed. This idea is that even if the user doesn’t want to record the arguments, that extra pass of keyword arguments wont’t cause an exception.
>
>
>
> Sorry for bothering you guys if this is a stupid idea.
>
> Happy to hear your suggestions.
>
>
> Li Mo
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181225/5cf6aa06/attachment.html>


More information about the Python-ideas mailing list