[Python-ideas] Python's Source of Randomness and the random.py module Redux

Random832 random832 at fastmail.com
Thu Sep 10 03:25:34 CEST 2015

Andrew Barnert via Python-ideas
<python-ideas at python.org> writes:

> You can work around that by,
> e.g., providing your own myrandom.py that does that and then using
> "from myrandom import random" everywhere, or by stashing a random_inst
> inside the random module or builtins or something and only creating it
> if it doesn't exist, etc., but all of these are things that people
> will rightly complain about.

Of course, this brings to mind the fact that there's *already* an
instance stashed inside the random module.

At that point, you might as well just keep the module-level functions,
and rewrite them to be able to pick up on it if you replace _inst
(perhaps suitably renamed as it would be a public variable) with an
instance of a different class.

Proof-of-concept implementation:

class _method:
    def __init__(self, name):
        self.__name__ = name
    def __call__(self, *args, **kwargs):
        return getattr(_inst, self.__name__)(*args, **kwargs)
    def __repr__(self):
        return "<random method wrapper " + repr(self.__name__) + ">"

_inst = Random()
seed = _method('seed')
random = _method('random')

More information about the Python-ideas mailing list