擅长:python、mysql、java
<p>你把问题诊断错了一点。问题是<code>random.Random</code>的初始化并没有完全包含在<code>random.Random.__init__</code>中。它还继承了<a href="https://hg.python.org/cpython/file/2.7/Modules/_randommodule.c#l498" rel="nofollow noreferrer">^{<cd3>}</a>:</p>
<pre><code>static PyObject *
random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
RandomObject *self;
PyObject *tmp;
if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
return NULL;
self = (RandomObject *)type->tp_alloc(type, 0);
if (self == NULL)
return NULL;
tmp = random_seed(self, args);
if (tmp == NULL) {
Py_DECREF(self);
return NULL;
}
Py_DECREF(tmp);
return (PyObject *)self;
}
</code></pre>
<p>您也将不得不重写<code>__new__</code>,并且只传递它期望的<code>seed</code>参数,位置上(因为它不将其理解为关键字参数)。在</p>
<p>它们真的不应该像这样混合<code>__init__</code>和{<cd4>}。当你这样做的时候,初始化顺序会变得很奇怪。在</p>