Raymond Hettinger <raymond.hettin...@gmail.com> added the comment:

One fix is to move up the code for converting step to istep and then modify the 
early out test to:  

        if stop is None and istep == 1:
            if istart > 0:
                return self._randbelow(istart)

That would have the downside of slowing down the common case.

Another possibility is changing the default step to a sentinel object and just 
testing for that:

        if stop is None and step is sentinel:
            if istart > 0:
                return self._randbelow(istart)

That would be user visible in the function signature but it would run fast.

We could bite the bullet and fully harmonize the randrange() signature with 
range().  That would entail switching to *args and no longer accepting keyword 
arguments:

     def randrange(self, /, *args):
         "Choose random item from range(stop) or range(start, stop[, step])."
         return self.choice(range(*args))

This would most closely match user expectations but is potentially a breaking 
change for existing code that uses keyword arguments.  Such code probably 
exists but is probably not common.

For speed, the actual implementation could still have fast paths for common 
cases.

For help() and tooltips, we could add a __text_signature__ to cover-up the 
*args.  However, signature objects currently aren't capable of describing 
range().  The best we could do is:

    randrange.__text_signature__ = '($self, start, stop, step, /)'

That would give help() that looks like this:

    >>> help(randrange)
    Help on method randrange in module Random:

    randrange(start, stop, step, /) method of __main__.R instance
        Choose random item from range(stop) or range(start, stop[, step]).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42772>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to