概述
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. """
我可以通过以下方式轻松找到random.py文件:
In [1]: import sys In [2]: print random.__file__ /usr/lib/python2.7/random.pyc
但是_random没有这个变量:
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__'
那么什么是_random,为什么随机子类化它,我在哪里可以找到它的相应文件?
_random.so
在Python使用的共享库的目录中.
>>> _random >>> <module '_random' from '/path/to/python/sharedlibs/_random.so'>
顺便说一句,并非所有可导入的模块都有与之关联的文件.有些是Python可执行文件的一部分,内置模块:
>>> 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')
所以,如果你进入你的平台:
>>> _random _random <module '_random' (built-in)>
比_random是Python可执行的一部分.
在C source _randommodule.c中,您可以找到可在Python中使用的Random方法:
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,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,PyDoc_STR("getrandbits(k) -> x. Generates an int with " "k random bits.")},{NULL,NULL} /* sentinel */ };
相比于:
>>> [x for x in dir(_random.Random) if not x.startswith('__')] ['getrandbits','getstate','jumpahead','random','seed','setstate']
总结
以上是编程之家为你收集整理的什么是python的_random?全部内容,希望文章能够帮你解决什么是python的_random?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧