- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想检查一个对象是否是某个类的实例。在 Python 中,我可以使用 instanceof
来做到这一点。在 C/C++ 中,我发现了一个名为 PyObject_IsInstance 的函数.但它似乎不像 isinstance
那样工作。
详细说明(也描述为下面的示例代码):
My
。类型定义为MyType
,对象定义为MyObject
。MyType
添加到名称为 My
的导出模块。my = My()
,isinstance(my, My)
返回 True
。PyObject_IsInstance(my, (PyObject*)&MyType)
来检查 my
,这将返回 0
,这意味着my
不是 MyType
定义的类的实例。完整的 C++ 代码:
#define PY_SSIZE_T_CLEAN
#include <python3.6/Python.h>
#include <python3.6/structmember.h>
#include <stddef.h>
typedef struct {
PyObject_HEAD
int num;
} MyObject;
static PyTypeObject MyType = []{
PyTypeObject ret = {
PyVarObject_HEAD_INIT(NULL, 0)
};
ret.tp_name = "cpp.My";
ret.tp_doc = NULL;
ret.tp_basicsize = sizeof(MyObject);
ret.tp_itemsize = 0;
ret.tp_flags = Py_TPFLAGS_DEFAULT;
ret.tp_new = PyType_GenericNew;
return ret;
}();
// check if obj is an instance of MyType
static PyObject *Py_fn_checkMy(PyObject *obj) {
if (PyObject_IsInstance(obj, (PyObject *)&MyType)) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
static PyMethodDef modmethodsdef[] = {
{ "checkMy", (PyCFunction)Py_fn_checkMy, METH_VARARGS, NULL },
{ NULL }
};
static PyModuleDef moddef = []{
PyModuleDef ret = {
PyModuleDef_HEAD_INIT
};
ret.m_name = "cpp";
ret.m_doc = NULL;
ret.m_size = -1;
return ret;
}();
PyMODINIT_FUNC
PyInit_cpp(void)
{
PyObject *mod;
if (PyType_Ready(&MyType) < 0)
return NULL;
mod = PyModule_Create(&moddef);
if (mod == NULL)
return NULL;
Py_INCREF(&MyType);
PyModule_AddObject(mod, "My", (PyObject *)&MyType);
PyModule_AddFunctions(mod, modmethodsdef);
return mod;
}
编译成cpp.so
,并在Python中测试:
>>> import cpp
>>> isinstance(cpp.My(), cpp.My)
True
>>> cpp.checkMy(cpp.My())
False
最佳答案
This is the typical calling convention, where the methods have the type PyCFunction. The function expects two
PyObject*
values. The first one is the self object for methods; for module functions, it is the module object. The second parameter (often called args) is a tuple object representing all arguments. This parameter is typically processed usingPyArg_ParseTuple()
orPyArg_UnpackTuple()
.
Py_fn_checkMy
的函数签名与此不匹配。它应该有两个参数。第一个是模块,这是您根据 MyType
检查的内容。第二个参数(您实际上并不接受)是一个包含您传递的对象的元组。您应该从元组中提取参数并检查其类型。
您最好使用 METH_O
来指定单个参数,而不是从元组中提取参数:
static PyObject *Py_fn_checkMy(PyObject *self, PyObject *obj) {
if (PyObject_IsInstance(obj, (PyObject *)&MyType)) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
static PyMethodDef modmethodsdef[] = {
{ "checkMy", (PyCFunction)Py_fn_checkMy, METH_O, NULL },
{ NULL }
};
关于Python C API : How to check if an object is an instance of a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56214129/
我是一名优秀的程序员,十分优秀!