This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger, serhiy.storchaka
Date 2020-12-28.19:57:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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]).
Date User Action Args
2020-12-28 19:57:18rhettingersetrecipients: + rhettinger, serhiy.storchaka
2020-12-28 19:57:18rhettingersetmessageid: <>
2020-12-28 19:57:18rhettingerlinkissue42772 messages
2020-12-28 19:57:18rhettingercreate