Message111554
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? |
|
Date |
User |
Action |
Args |
2010-07-25 19:18:53 | lukasz.langa | set | recipients:
+ lukasz.langa, rhettinger, BreamoreBoy |
2010-07-25 19:18:53 | lukasz.langa | set | messageid: <1280085533.93.0.218883892309.issue9379@psf.upfronthosting.co.za> |
2010-07-25 19:18:52 | lukasz.langa | link | issue9379 messages |
2010-07-25 19:18:51 | lukasz.langa | create | |
|