[Python-3000] Cleaning up argument list parsing (was Re: More wishful thinking)
Ian Bicking
ianb at colorstudy.com
Wed Apr 19 19:40:34 CEST 2006
Guido van Rossum wrote:
> On 4/19/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
>>Guido van Rossum wrote:
>>
>>>Here's a related but more complicated wish: define a function in such
>>>a way that certain parameters *must* be passed as keywords, *without*
>>>using *args or **kwds. This may require a new syntactic crutch.
>>
>>A single '*' could indicate that no additional positional arguments were
>>permitted.
>>
>>def f(*, paramA, paramB=10):
>> print paramA, paramB
>>
>>Here paramA and paramB can only be passed as keywords, and paramA *has* to be
>>passed as it has no default.
>
>
> I once considered and rejected this syntax since another logical
> interpretation would be that any positional arguments are accepted but
> *ignored*.
While I guess I can understand why that might be interpreted that way,
such an interpretation also assume the feature is pretty worthless; why
would you ignore the arguments, instead of just ignore the variable the
arguments were put into?
>>Utterly magical to anyone not already familiar with the use of *args, though.
>
>
> But so would any other newly invented syntax, probably, so that
> doesn't count heavily.
>
> Would *None be too bizarre?
Hmm...
def f(*(), paramA, paramB=10):
*() would kind of imply you have to unpack the positional parameters
into (), and of course the only thing that can be unpacked to () is ().
Kind of building on the idea that f(*(a, b)) and f(a, b) are
equivalent calls, even though in a signature *(a, b) isn't actually
allowed (being pointless anyway).
But while it has some logical meaning, I don't know if it reads
particularly well; too many ()'s, and most people don't encounter empty
tuples anyway, and probably don't think of signatures as packing and
unpacking. * alone and *None read better to me. * by itself feels most
like it is splitting the positional arguments from the keyword arguments:
def write_text(s, *, font=None, size=None, color=None, ...): ...
The primary use case I see is with a method that has lots of keyword
arguments (4+), at which point no one is going to remember the order of
the arguments, including people implementing functions with the same
signature. With a lot of keyword arguments, **(font=None, size=None,
...) (noted in another message) doesn't feel as nice... though actually
as I think about it, maybe that's not true, maybe it does look just as nice.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-3000
mailing list