function was added to the
is a sequence or
object containing the elements of a population, and
chooses k elements from the population without replacing chosen
elements. k can be any value up to
. For example:
>>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn']
>>> random.sample(days, 3) # Choose 3 elements
['St', 'Sn', 'Th']
>>> random.sample(days, 7) # Choose 7 elements
['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']
>>> random.sample(days, 7) # Choose 7 again
['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']
>>> random.sample(days, 8) # Can't choose eight
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "random.py", line 414, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population
>>> random.sample(xrange(1,10000,2), 10) # Choose ten odd nos. under 10000
[3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195]
module now uses a new algorithm, the Mersenne Twister, implemented
in C. It’s faster and more extensively studied than the previous algorithm.
(All changes contributed by Raymond Hettinger.)
module also gained a number of new functions:
modules have been declared dead, and attempts to import
them will fail with a
. New-style classes provide new ways to break out
of the restricted execution environment provided by
, and no one has interest
in fixing them or time to do so. If you have applications using
, rewrite them to
use something else.
(Sticking with Python 2.2 or 2.1 will not make your applications any safer because
there are known bugs in the
module in those versions. To repeat: if you’re
, stop using it immediately.)
module has been deprecated because the algorithm it uses for encryption
is not believed to be secure. If you need encryption, use one of the several AES
Python modules that are available separately.
module gained a
function that recursively moves a
file or directory to a new location.