[Python-checkins] cpython (2.7): Issue 14927: Minor clean-up of function parameters in random().

raymond.hettinger python-checkins at python.org
Sun Oct 6 06:35:03 CEST 2013


http://hg.python.org/cpython/rev/b1e94e332ec8
changeset:   85987:b1e94e332ec8
branch:      2.7
parent:      85983:101bf827611a
user:        Raymond Hettinger <python at rcn.com>
date:        Sat Oct 05 21:34:48 2013 -0700
summary:
  Issue 14927: Minor clean-up of function parameters in random().

files:
  Lib/random.py |  39 +++++++++++++++++++--------------------
  1 files changed, 19 insertions(+), 20 deletions(-)


diff --git a/Lib/random.py b/Lib/random.py
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -170,29 +170,28 @@
 
 ## -------------------- integer methods  -------------------
 
-    def randrange(self, start, stop=None, step=1, int=int, default=None,
-                  maxwidth=1L<<BPF):
+    def randrange(self, start, stop=None, step=1, _int=int, _maxwidth=1L<<BPF):
         """Choose a random item from range(start, stop[, step]).
 
         This fixes the problem with randint() which includes the
         endpoint; in Python this is usually not what you want.
-        Do not supply the 'int', 'default', and 'maxwidth' arguments.
+
         """
 
         # This code is a bit messy to make it fast for the
         # common case while still doing adequate error checking.
-        istart = int(start)
+        istart = _int(start)
         if istart != start:
             raise ValueError, "non-integer arg 1 for randrange()"
-        if stop is default:
+        if stop is None:
             if istart > 0:
-                if istart >= maxwidth:
+                if istart >= _maxwidth:
                     return self._randbelow(istart)
-                return int(self.random() * istart)
+                return _int(self.random() * istart)
             raise ValueError, "empty range for randrange()"
 
         # stop argument supplied.
-        istop = int(stop)
+        istop = _int(stop)
         if istop != stop:
             raise ValueError, "non-integer stop for randrange()"
         width = istop - istart
@@ -210,14 +209,14 @@
             # a long, but we're supposed to return an int (for backward
             # compatibility).
 
-            if width >= maxwidth:
-                return int(istart + self._randbelow(width))
-            return int(istart + int(self.random()*width))
+            if width >= _maxwidth:
+                return _int(istart + self._randbelow(width))
+            return _int(istart + _int(self.random()*width))
         if step == 1:
             raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
 
         # Non-unit step argument supplied.
-        istep = int(step)
+        istep = _int(step)
         if istep != step:
             raise ValueError, "non-integer step for randrange()"
         if istep > 0:
@@ -230,9 +229,9 @@
         if n <= 0:
             raise ValueError, "empty range for randrange()"
 
-        if n >= maxwidth:
+        if n >= _maxwidth:
             return istart + istep*self._randbelow(n)
-        return istart + istep*int(self.random() * n)
+        return istart + istep*_int(self.random() * n)
 
     def randint(self, a, b):
         """Return random integer in range [a, b], including both end points.
@@ -240,7 +239,7 @@
 
         return self.randrange(a, b+1)
 
-    def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
+    def _randbelow(self, n, _log=_log, _int=int, _maxwidth=1L<<BPF,
                    _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
         """Return a random int in the range [0,n)
 
@@ -257,7 +256,7 @@
             # has not been overridden or if a new getrandbits() was supplied.
             # This assures that the two methods correspond.
             if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
-                k = int(1.00001 + _log(n-1, 2.0))   # 2**k > n-1 > 2**(k-2)
+                k = _int(1.00001 + _log(n-1, 2.0))   # 2**k > n-1 > 2**(k-2)
                 r = getrandbits(k)
                 while r >= n:
                     r = getrandbits(k)
@@ -265,7 +264,7 @@
         if n >= _maxwidth:
             _warn("Underlying random() generator does not supply \n"
                 "enough bits to choose from a population range this large")
-        return int(self.random() * n)
+        return _int(self.random() * n)
 
 ## -------------------- sequence methods  -------------------
 
@@ -273,20 +272,20 @@
         """Choose a random element from a non-empty sequence."""
         return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
 
-    def shuffle(self, x, random=None, int=int):
+    def shuffle(self, x, random=None):
         """x, random=random.random -> shuffle list x in place; return None.
 
         Optional arg random is a 0-argument function returning a random
         float in [0.0, 1.0); by default, the standard random.random.
 
-        Do not supply the 'int' argument.
         """
 
         if random is None:
             random = self.random
+        _int = int
         for i in reversed(xrange(1, len(x))):
             # pick an element in x[:i+1] with which to exchange x[i]
-            j = int(random() * (i+1))
+            j = _int(random() * (i+1))
             x[i], x[j] = x[j], x[i]
 
     def sample(self, population, k):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list