What is python _random?

If you open random.py to see how it works, its Random class subclasses _random.Random :

 import _random class Random(_random.Random): """Random number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. Especially useful for multi-threaded programs, creating a different instance of Random for each thread, and using the jumpahead() method to ensure that the generated sequences seen by each thread don't overlap. Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the following methods: random(), seed(), getstate(), setstate() and jumpahead(). Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges. """ 

I can easily find the random.py file by doing:

 In [1]: import sys In [2]: print random.__file__ /usr/lib/python2.7/random.pyc 

However, _random does not have this variable:

 In [3]: _random.__file__ --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-295-a62b7df330e2> in <module>() ----> 1 _random.__file__ AttributeError: 'module' object has no attribute '__file__' 

So what is _random , why does Random subclass it, and where can I find its corresponding file?

+6
source share
2 answers

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'] 
+6
source

This is a reference to C Python _ arbitrary module . It is implemented in C, so there is no .py file to search for.

+3
source

Source: https://habr.com/ru/post/1013766/


All Articles