[Python-ideas] About the passing the function arguments in Keyword form.
steve at pearwood.info
Mon Dec 24 06:24:32 EST 2018
On Mon, Dec 24, 2018 at 06:21:31PM +0800, 李默 wrote:
> I am having an idea on loosing the argument validity check when passing the function arguments in keyword way.
> For example:
> deff(x, y):
> print(x, y)
> f(x=7, y=9, z=9)
> In the current of python, the extra pass of 'z' would let the
> interpreter raise an exception and stop work.
Correct. As the Zen of Python says:
Errors should never pass silently.
Passing an unexpected argument "z" is an error, regardless of whether
you pass it by keyword or as a positional argument. It should raise an
Don't think about toy examples like your f above with single character
names. Think about code with proper names:
def download(url, output_file=None, overwrite=True):
if output_file is None:
output_file = generate_filename(url)
# Oops, a typo, which silently deletes data.
> 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
I don't agree that it is reasonable.
To quote Chris Smith:
"I find it amusing when novice programmers believe their
main job is preventing programs from crashing. ... More
experienced programmers realize that correct code is
great, code that crashes could use improvement, but
incorrect code that doesn’t crash is a horrible nightmare."
Functions which silently ignore unexpected arguments instead of telling
us that we have made a mistake ("got an unexpected keyword argument z")
just *hides* the error, instead of reporting it so we can fix it.
> And this modification can bring more freedom of programming.
Freedom to have more hard to diagnose bugs in our code.
More information about the Python-ideas