[Python-Dev] Signature of function with default value uncapturable in Python and C

Terry Reedy tjreedy at udel.edu
Thu Jan 16 04:14:23 CET 2014

On 1/15/2014 9:25 PM, Vajrasky Kok wrote:
> Dear friends,
>>>> from itertools import repeat
>>>> list(repeat('a', 3))
> ['a', 'a', 'a']
>>>> list(repeat('a', 0))
> []
>>>> repeat.__doc__
> 'repeat(object [,times]) -> create an iterator which returns the
> object\nfor the specified number of times.  If not specified, returns
> the object\nendlessly.'

I think that the doc should say that a negative value is treated as 0 
and that this is enough for a tracker issue after you get more feedback 
or gather more info. There is at least one other builtin/stdlib function 
that does this.

> If you omit the times argument:
>>>> list(repeat('a'))
> ... unlimited of a .... sometimes it can hang your machine ....
> In the C code it self, the default value of variable handling times
> argument is -1.

Is is necessary to give times a pseudo-default? What is done in other 
places (which are many) where a parameter is optional, with no default?

> It checks how many arguments you give to the function.
> So if you give -1 directly:
>>>> list(repeat('a', -1))
> []
> Negative value of times argument means 0.
> So what is the correct signature of this function? The value is not
> really capturable in Python and C.

The signature in the doc is correct: times is optional, with no default 
value. Instead, the function has a default behavior that does not need 
the value. There are other examples. The (nearly) 'equivalent' Python 
code in the doc fakes this with times=None, but passing None fails. I 
think the same issue occurs in the random module.

> repeat(object [,times = unlimited]) ????
> Can we do this in Clinic? If not, should we?

I should hope that Clinic (and signature objects) can handle no-default 
optional args, as there are several.

Terry Jan Reedy

More information about the Python-Dev mailing list