[Python-3000] PEP 3102
Arnaud Delobelle
arnodel at googlemail.com
Mon Feb 18 21:17:41 CET 2008
On 18 Feb 2008, at 19:39, Talin wrote:
> Arnaud Delobelle wrote:
>>
[...]
>> Unconvincingly yours,
>
> Well, that is certainly a logical continuation of the train of
> thought behind the 'single *' syntax.
> I'd be curious to know which parts of 3102 people are finding most
> useful. You see, the itch that I was originally attempting to
> scratch concerned just the narrow use case of combining a 'varargs'
> function with optional keywords. I've seen/written a lot of code
> where you have a function that takes a list of arguments, with a
> couple of optional keywords that control how that list is handled,
> and 3102 was intended to make that case much easier to handle.
Another use would be allowing the '_cache trick' with a varargs
function, i.e.
def f(*args, _cache={}):
...
Personally I don't like this trick though...
> I was never all that interested in 'forcing' some of the arguments
> to a non-varargs function to be specified by keyword. I am perfectly
> comfortable with the idea that the caller always gets to choose
> whether to pass something positionally or via keyword, and limiting
> the caller's choice seems vaguely unpythonic to me. However, I
> included that part in the PEP based on the feedback from here.
>
> In any case, limiting arguments to being specified by keyword-only
> or positional-only was not part of my original 'itch', and I am
> curious as to what are the use cases for it that people are
> envisioning.
>
A typical use of positional-only arguments is with a function
def f(x, y=1, **kwargs):
...
where keyword arguments are potentially anything at all, including x
and y. For example: dict.update(). In fact it is a fairly
symmetrical itch to yours: without positional only arguments, the
above must be written something like:
def f(*args, **kwargs):
nargs = len(args)
if 1 <= nargs <= 2:
x = args[0]
y = args[1] if nargs==2 else 1
else:
raise blah
...
whereas with positional-only arguments (using the syntax I put
forward) one could write simply:
def f(**kwargs, x, y=1)
Just like
def f(*args, a, b=1):
...
had to be written pre-PEP 3102 as something like:
def f(*args, **kwargs):
a = kwargs.pop('a')
b = kwargs.pop('b', 1)
if kwargs:
raise blah
Both itches are about not having to unpack something: **kwargs in the
case of PEP 3102, *args in the case of positional-only arguments.
--
Arnaud
More information about the Python-3000
mailing list