[Python-3000] PEP3102 Keyword-Only Arguments (original) (raw)

Guido van Rossum guido at python.org
Mon Aug 14 19:38:25 CEST 2006


Not remembering the PEP in detail, I agree with Jim's resolution of all these.

I guess the right rule is that all positional arguments come first (first the regular ones, then * or *args). Then come the keyword arguments, again, first the regular ones (name=value), then **kwds.

I believe the PEP doesn't address the opposite use case: positional arguments that should not be specified as keyword arguments. For example, I might want to write

def foo(a, b): ...

but I don't want callers to be able to call it as foo(b=1, a=2) or even foo(a=2, b=1).

A realistic example is the write() method of file objects. We really don't want people starting to say f.write(s="abc") because even if that works for the current file type you're using, it won't work if an instance of some other class implementing write() is substituted -- write() is always documented as an API taking a positional argument, so different "compatible" classes are likely to have different argument names. Currently this is enforced because the default file type is implemented in C and it doesn't have keyword arguments; but in Py3k it may well be implemented in Python and then we currently have no decent way to say "this should really be a positional argument". (There's an analogy to forcing keyword arguments using **, using *args for all arguments and parsing that explicitly -- but that's tedious for a fairly common use case.)

Perhaps we can use ** without following identifier to signal this? It's not entirely analogous to * without following identifier, but at least somewhat similar.

--Guido

On 8/12/06, Jim Jewett <jimjjewett at gmail.com> wrote:

On 8/11/06, Jiwon Seo <seojiwon at gmail.com> wrote: > When we have keyword-only arguments, do we allow 'keyword dictionary' > argument? If that's the case, where would we want to place > keyword-only arguments?

> Are we going to allow any of followings? > 1. def foo(a, b, *, key1=None, key2=None, **map) Seems perfectly reasonable. I think the controversy was over whether or not to allow keyword-only without a default. > 2. def foo(a, b, *, **map, key1=None, key2=None) Seems backward, though I suppose we could adjust if we needed to. > 3. def foo(a, b, *, **map) What would the * even mean, since there aren't any named keywords to separate? -jJ


Python-3000 mailing list Python-3000 at python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-3000 mailing list