[Python-Dev] AC Derby and accepting None for optional positional arguments

Larry Hastings larry at hastings.org
Thu Jan 16 06:55:46 CET 2014

On 01/15/2014 09:37 PM, Guido van Rossum wrote:
> Well, I think these are mostly artifacts from old times, and usually 
> passing None *should* be the same as omitting the argument. But check 
> each case!

Vajrasky Kok's recent posting to python-dev discusses the same problem.  
His example is itertools.repeat's second parameter, which is slightly 
nastier.  Consider the implementation:

    static PyObject *
    repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         repeatobject *ro;
         PyObject *element;
         Py_ssize_t cnt = -1;
         static char *kwargs[] = {"object", "times", NULL};

         if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
                                          &element, &cnt))
             return NULL;

         if (PyTuple_Size(args) == 2 && cnt < 0)
             cnt = 0;

I draw your attention to the last two lines.  And remember, Argument 
Clinic doesn't provide the "args" and "kwargs" parameters to the "impl" 
function.  That means two things:

  * itertools.repeat deliberately makes it impossible to provide an
    argument for "times" that behaves the same as not providing the
    "times" argument, and
  * there is currently no way to implement this behavior using Argument
    Clinic.  (I'd have to add a hack where impl functions also get args
    and kwargs.)

Passing in "None" here is inconvenient as it's an integer argument. -1 
actually seems like a pretty sane default to mean "repeat forever", but 
the author has gone to some effort to prevent this.  I therefore assume 
they had a very good reason.  So again we seem stuck.

Are you suggesting that, when converting builtins to Argument Clinic 
with unrepresentable default values, we're permitted to tweak the 
defaults to something representable?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140115/c4051730/attachment.html>

More information about the Python-Dev mailing list