[Types-sig] Keyword arg declarations

Greg Stein gstein@lyra.org
Fri, 17 Dec 1999 12:04:37 -0800 (PST)

On Fri, 17 Dec 1999, David Ascher wrote:
> From: "Guido van Rossum" <guido@CNRI.Reston.VA.US>
> > I just realized that Tim's decl syntax that's currently being bandied
> > around doesn't declare the names of arguments.  That's fine for a
> > language like C, but in Python, any argument with a name (*args
> > excluded) can be used as a keyword argument.

I responded to this elsewhere; I believe we can easily declare varargs and
keywords with an unambigous syntax.

> An example of such a signature is familiar to all is the signature for
> range().  The docstring for range reads:
> range([start,] stop[, step]) -> list of integers
> which is not expressible with the current syntax.  A Python version of range
> would have to do, much like NumPy's arange does,

I believe it is expressible:

def range(start: Int, stop=None: Int or None, step=1: Int) -> [Int]:

The only caveat is that somebody could do:

  range(3, None, 1)

Which (technically) is not supposed to be allowed.

> Now, the builtin typechecker can of course be told about __builtin__.range's
> signature peculiarities, but is there any way we can address the more
> general problem?  Or is it, as I suspect, rare enough that one can ignore
> it?

Well, the above isn't necessarily the prettiest, but it *is* possible with
at least one proposal for syntax extensions. I believe this kind of
argument funkiness is pretty rare and we don't need to provide any special
handling or consideration for the problem.

> > (Note that not all builtins support keyword arguments; in fact most
> > don't.)
> And a shame it is, IMO.  Would it make sense to consider for 2.0 a mechanism
> which allows keyword arguments almost by default?  That way I could do

I think the builtins need to start using PyArg_ParseTupleAndKeywords(). I
seem to recall that there have been problems with that function in the
past, but I think they've been cleared up in the 1.5 series. That should
keyword-enable those functions...


Greg Stein, http://www.lyra.org/