gpt4 book ai didi

Python 的 __radd__ 不适用于 C 定义的类型

转载 作者:行者123 更新时间:2023-11-28 18:46:54 26 4
gpt4 key购买 nike

当使用 __radd__ 方法创建定义 noddy.Noddy 类型的 python (2.7.5) 扩展时,它会获得与(否则等效)python 不同的行为具有自定义 __radd__ 的已定义类对象(前者不起作用,而后者起作用)。示例:

class PythonClass():
def __radd__(self, other):
return 'indeed!'

w = PythonClass()
d = noddy.Noddy()

print(w.__radd__)
print(d.__radd__)

print('the following works:')
print([1] + w)
print('the following does not work:')
print([1] + d)

以及相应的输出:

<bound method PythonClass.__radd__ of <__main__.PythonClass instance at 0xf6e9792c>>
<built-in method __radd__ of noddy.Noddy object at 0xf749d4b8>
the following works:
indeed!
the following does not work:
Traceback (most recent call last):
File "examples/2.py", line 44, in <module>
print([1] + d)
TypeError: can only concatenate list (not "noddy.Noddy") to list

d.__radd__ 方法没有被调用,但是 w.__radd__ 被调用了。关于为什么会这样的任何想法? [1] + x 的行为,其中 x 是一个 PythonClass 实例,似乎符合 documentaton ,我希望 noddy.Noddy 也能正常工作。此外,两者都是与 list 无关的类型。

欢迎解决方法。我已经尝试用 forbiddenfruit 修补 list.__radd__ ,但没有成功,尽管我已将此问题提请作者注意,他恰好是我的密友。

编辑

...这是 C 地的照片:

typedef struct {
PyObject_HEAD
} Noddy;


static PyObject*
Noddy_radd(PyObject* _self, PyObject* args) {
printf("Noddy_radd!\n");
return NULL;
}

static PyObject*
Noddy_add(PyObject* _self, PyObject* args) {
printf("Noddy_add\n");
return NULL;
}

PyNumberMethods noddy_nums = {
Noddy_add, /* binaryfunc nb_add; /* __add__ */
0, /* binaryfunc nb_subtract; /* __sub__ */
0, /* binaryfunc nb_multiply; /* __mul__ */
0, /* binaryfunc nb_divide; /* __div__ */
0, /* binaryfunc nb_remainder; /* __mod__ */
0, /* binaryfunc nb_divmod; /* __divmod__ */
0, /* ternaryfunc nb_power; /* __pow__ */
0, /* unaryfunc nb_negative; /* __neg__ */
0, /* unaryfunc nb_positive; /* __pos__ */
0, /* unaryfunc nb_absolute; /* __abs__ */
0, /* inquiry nb_nonzero; /* __nonzero__ */
0, /* unaryfunc nb_invert; /* __invert__ */
0, /* binaryfunc nb_lshift; /* __lshift__ */
0, /* binaryfunc nb_rshift; /* __rshift__ */
0, /* binaryfunc nb_and; /* __and__ */
0, /* binaryfunc nb_xor; /* __xor__ */
0, /* binaryfunc nb_or; /* __or__ */
0, /* coercion nb_coerce; /* __coerce__ */
0, /* unaryfunc nb_int; /* __int__ */
0, /* unaryfunc nb_long; /* __long__ */
0, /* unaryfunc nb_float; /* __float__ */
0, /* unaryfunc nb_oct; /* __oct__ */
0, /* unaryfunc nb_hex; /* __hex__ */
};

static PyMethodDef Noddy_methods[] = {
{"__radd__", (PyCFunction)Noddy_radd, METH_VARARGS,
"__radd__ function"},
{NULL} /* Sentinel */
};


static PyTypeObject NoddyType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"noddy.Noddy", /*tp_name*/
sizeof(Noddy), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
&noddy_nums, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_HAVE_SEQUENCE_IN | /* tp_flags */
Py_TPFLAGS_HAVE_ITER,
"Noddy objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Noddy_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
PyType_GenericNew, /* tp_new */
};

最佳答案

Python 的 getattr 是个棘手的家伙。 __radd__ 方法是 [in] 著名魔法方法的一部分。它们不存储在与常规方法 (ob_type->tp_methods) 相同的数组中,它是 ob_type->tp_as_number 的一部分,由 Number Protocol 单独管理。

Forbidden fruit 有一个问题,要求能够对这些方法进行猴子修补。这个努力是documented here

关于Python 的 __radd__ 不适用于 C 定义的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18794169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com