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 lukasz.langa
Recipients BreamoreBoy, lukasz.langa, rhettinger
Date 2010-07-25.19:18:51
SpamBayes Score 0.00037027636
Marked as misclassified No
Message-id <1280085533.93.0.218883892309.issue9379@psf.upfronthosting.co.za>
In-reply-to
Content
Two behaviour problems with random.randrange:

1. Method argument `start` behaves as `stop` if `stop` is not defined:
======================================================================

>>> from random import randrange
>>> help(randrange)
Help on method randrange in module random:

randrange(self, start, stop=None, step=1, int=<class 'int'>, default=None, maxwidth=9007199254740992) method of random.Random instance
    Choose a random item from range(start, stop[, step]).

>>> randrange(10) #start=10
2

Clearly this is because randrange() mimicks the range() interface. Sphinx documentation specifies the behaviour clearly. The problem is, help() can mislead someone in this case.



2. `step` does not work when only `start` (acting as `stop`) specified:
=======================================================================

>>> [randrange(0, 10, step=5) for i in range(10)]
[5, 5, 5, 0, 5, 5, 5, 0, 0, 5]
>>> [randrange(10, step=5) for i in range(10)]
[5, 2, 4, 4, 6, 2, 7, 1, 5, 7]

One would expect the latter to work the same way as the former.



What next
=========

Case 2 is clearly a bug that should be addressed. Raymond, Mark - would a patch for this be accepted for 2.7.x, 3.1.x and 3.2? If so I can provide one.

In Case 1 we can do one of two things (or both):
A. Tweak the docstring to specify the actual behaviour explicitly.
B. Change the function definition to: `randrange(self, limit, *args, **kwargs)`. This is backwards compatible if we'd process `args` and `kwargs` correctly to keep the current interface intact (e.g. `start` would be processed in `kwargs`). This would leave a more predictable help().

Raymond, Mark - I'd say we absolutely do A. Does any of you object about B?
History
Date User Action Args
2010-07-25 19:18:53lukasz.langasetrecipients: + lukasz.langa, rhettinger, BreamoreBoy
2010-07-25 19:18:53lukasz.langasetmessageid: <1280085533.93.0.218883892309.issue9379@psf.upfronthosting.co.za>
2010-07-25 19:18:52lukasz.langalinkissue9379 messages
2010-07-25 19:18:51lukasz.langacreate