Re: keyword-arguments

Marc Wachowitz (mw@ipx2.rz.uni-mannheim.de)
28 May 1994 12:55:18 GMT

Donald Beaudry (don@vicorp.com) wrote:
> What do I mean by a keyword-argument extention...
[...]
> frobnicate(f, b, z = my_z)
>
> and not have to worry about the defaults. A feature like this would
> make it practical to write and use functions that have a many default
> parameter values.

I think this would be a good idea, except that I'd prefer a safer syntax
avoiding places where both "=" and "==" would be valid Python code; e.g.
use a colon instead (similar to dictionaries). Btw, if you want a such a
feature now, for functions with many arguments (default or not), you can
already do it with one dictionary argument (which by default is "{}"). I
think it would also be better to accept keyword arguments only in places
where the programmer of the function/method announced it, since it would
otherwise create many implicit contracts between a function and callers.
I'd propose to use a colon for this, too (in place of "=" for defaults).

As a fix for the "caller doesn't want to know about defaults" problem, I
do often use "None" as default, and set the real default if the argument
is actually None. Thus the caller need not worry - and the default might
have to be a dynamic value (like sys.stdin at call-time) anyway. Keyword
parameters should only be allowed after positional parameters (whether a
positional paremeter is optional or not doesn't matter for this rule). A
safer creation of pseudo-closures (where defaults are merely meant to be
remembered values, but should never be changed by the caller) could also
be indicated by using "is" instead of "=" and should occur after all the
other parameters, even the rest-parameter marked with "*". The parameter
order should then be: required, position-default, keyword-default, rest,
pseudo-closure. The size-overhead in functions for such additions would,
in any reasonably large application, not matter much. With special codes
for various combinations of parameter styles it might also be no problem
in terms of call-time overhead (i.e. it only costs where it is used, and
there it would certainly be faster than a simulation in Python code).

Example for using keyword parameters:

def display(text, window: None, font: None, width: None, height: None):
if window is None: window = window_manager.current_window
if font is None: font = window.default_font
if width is None: width = compute_default_width(window, font)
if height is None: height = compute_default_height(window, font)
# ... actually I don't write code for GUIs (yet) ;-)

display("Hello world!", font: "Helvetica")

------------------------------------------------------------------------------
* wonder everyday * nothing in particular * all is special *
Marc Wachowitz <mw@ipx2.rz.uni-mannheim.de>