[Python-Dev] Optional positional-only parameters (was Re: [Python-3000] PEP 3102)

Joel Bender jjb5 at cornell.edu
Tue Feb 19 16:53:43 CET 2008


Nick Coghlan wrote:

> We've also veered fairly far off topic for the Py3k list - further ideas 
> for positional-only argument syntax or decorators should probably be 
> kicked around on python-ideas rather than here or python-dev.

For a function specification like this:

     def f(w, x=1, *, y, z=2): ...

My preference is for w and x to be positional only, w is required; y and 
z are keyword only, y is required.

I personally like the idea that for functions like range([start,] stop 
[,step]) that the function describes which combinations of positional 
functions are allowed.  An alternative would be overloading, which I 
would use, albeit rarely:

     def _range(x, y, z): ...
     def range(y): return _range(None, y, None)
     def range(x, y): return _range(x, y, None)
     def range(x, y, z): return _range(x, y, z)

As for this relative nonsense:

     def test([arg1=1, [[*arg2,] arg3=3,]] arg4): ...

Someday I'll dig around in the interpreter enough to make this work, 
just to see what it would be like.  But not today.

 > 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...

My preference for defining persistent variables with a local scope would 
be to introduce a "local" or "static" keyword like "global":

     def f(*args):
         static cache={}
         ...

But I'm sure that that has been discussed before.


Joel


More information about the Python-Dev mailing list