Common practice is to use underscores for modules implemented in C. Often, the _mod
template is _mod
for this C module and the mod
for the Python module that imports this _mod
. You will find this for several modules of the standard library. Generally, you should use mod
, not _mod
.
Mac OS X has a file:
_random.so
In the shared library directory used by Python.
Just enter the module name in the interactive prompt to see the path:
>>> _random >>> <module '_random' from '/path/to/python/sharedlibs/_random.so'>
By the way, not all modules that you can import have a file associated with them. Some of them are part of the Python executable, built-in 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 to your platform:
>>> _random _random <module '_random' (built-in)>
Than _random
is part of the Python executable itself.
In the C source _randommodule.c, you can find the Random
methods that become 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 with:
>>> [x for x in dir(_random.Random) if not x.startswith('__')] ['getrandbits', 'getstate', 'jumpahead', 'random', 'seed', 'setstate']