What is python's _random?
It is common practice to use a leading underscore for modules implemented in C. Often the pattern _mod
for this C module and mod
for a Python module that imports this _mod
is used. You will find this for several modules of the standard library. Typically, you should use mod
and not _mod
.
On Mac OS X there is a file:
_random.so
In the directory of the shared libraries used by Python.
Just type the module name at the interactive prompt to see the path:
>>> _random
>>> <module '_random' from '/path/to/python/sharedlibs/_random.so'>
BTW, not all modules you can import have a file associated with them. Some are part of the Python executeable, the builtin modules:
>>> import sys
>>> sys.builtin_module_names
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale',
'_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread',
'_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno',
'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys',
'time', 'xxsubtype', 'zipimport')
So if you get on your platform:
>>> _random
_random <module '_random' (built-in)>
Than _random
is part of Python executeable itself.
In the C source _randommodule.c you can find the methods of Random
that are made available for use in Python:
static PyMethodDef random_methods[] = {
{"random", (PyCFunction)random_random, METH_NOARGS,
PyDoc_STR("random() -> x in the interval [0, 1).")},
{"seed", (PyCFunction)random_seed, METH_VARARGS,
PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
{"getstate", (PyCFunction)random_getstate, METH_NOARGS,
PyDoc_STR("getstate() -> tuple containing the current state.")},
{"setstate", (PyCFunction)random_setstate, METH_O,
PyDoc_STR("setstate(state) -> None. Restores generator state.")},
{"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
PyDoc_STR("getrandbits(k) -> x. Generates an int with "
"k random bits.")},
{NULL, NULL} /* sentinel */
};
Compare to:
>>> [x for x in dir(_random.Random) if not x.startswith('__')]
['getrandbits', 'getstate', 'jumpahead', 'random', 'seed', 'setstate']
It's a reference to C Python's _random module. It is implemented in C, so there is no .py file to find.