- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用 __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/
我希望我的 Fraction 类在添加到 float 或整数时作为 float 工作,这样我就可以自然地用它执行操作,但它仅在 Fraction 时才起作用> 是最右边的操作数。有没有办法让它以任何顺
你好 试图理解 __radd__ 是如何工作的。我有代码 >>> class X(object): def __init__(self, x): self.x = x
我目前正在学习 python 运算符重载(准确地说是 __radd__ 和 __add__),我有以下代码 class Commuter1: def __init__(self, val):
我正在(作为练习)尝试制作一个使用罗马数字的类(class)。我试图将尽可能多的功能放入其中,所以我考虑让运算符(operator)在它们上工作。我发现(例如,问题在其他方法对中仍然存在)__add_
在 python 中,您可以覆盖右(和左)加法运算符 ( + ): 让我们将这个虚拟对象用于以下目的: class Dummy(object): pass ## dummy object 这是一
当使用 __radd__ 方法创建定义 noddy.Noddy 类型的 python (2.7.5) 扩展时,它会获得与(否则等效)python 不同的行为具有自定义 __radd__ 的已定义类对象
这个问题在这里已经有了答案: Custom class with __add__ to add with NumPy array (1 个回答) 关闭 9 个月前。 考虑以下 python 代码:
假设我们写一个小类: class K: pass obj = K() 是下面的代码... total = 4 + obj ...本质上和下面一样? import io try: tot
str 还有其他方法,如 __rmod__、__rsub__ 或 __rmul__ int 确实有一个 __radd__ 方法 很奇怪,我想知道为什么。 当第一个学期有 add 方法时调用的 radd
在Python中,如果我想要一个实例直接返回字段的字符串,我可以这样做: class X: def __init__(self, field): self.field = fi
我是一名优秀的程序员,十分优秀!