Message346106
Why this code uses int() at all? My guess is that it was added for earlier detection of user errors, otherwise the user could get a TypeError or AttributeError with unrelated message, or even a wrong result. int() is used just for type checking. But it is not good tool for this. int() does several different actions:
1. Parses string representation of integer.
2. Converts a real number to an integer with some truncation.
3. Losslessly converts an int-like number into an instance of int.
The first two options are not applicable here, so we need an additional check
if istart != start:
raise ValueError("non-integer arg 1 for randrange()")
And this is not perfect: for randrange('5') we get a ValueError instead of expected TypeError.
operator.index() is better tool for this. |
|
Date |
User |
Action |
Args |
2019-06-20 07:43:34 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, tim.peters, rhettinger, mark.dickinson |
2019-06-20 07:43:34 | serhiy.storchaka | set | messageid: <1561016614.29.0.603291486969.issue37319@roundup.psfhosted.org> |
2019-06-20 07:43:34 | serhiy.storchaka | link | issue37319 messages |
2019-06-20 07:43:34 | serhiy.storchaka | create | |
|