本文介绍了如何使用PyObject_IsInstance与非内置类作为第二个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / C ++,我想看看是否的PyObject 是一个实例。不幸的是, PyInstance_Check 宏不与新样式类的工作。

In C/C++, I want to see if a PyObject is an instance. Unfortunately, the PyInstance_Check macro doesn't work with new-style classes.

所以,照我看论坛上的帖子, PyObject_IsInstance 可以解决这个问题。然而,我发现正在展示具有内置类型的比较的所有例子,像整数和字符串。

So, according to forums posts that I read, PyObject_IsInstance could solve the problem. However, all the examples that I have found are demonstrating comparisons with built-in types, like ints and strings.

我想知道我怎么可以构造一个的PyObject是重新presents类的类型,这样我就可以把它传递给 PyObject_IsInstance 的第二个参数。你能帮助我吗?

I want to know how I can construct a PyObject that represents a class of a type, so I can pass it to the second argument of PyObject_IsInstance. Can you help me?

推荐答案

您可以使用相应的 PyTypeObject 如果同一模块中定义。如果类型的对象(假设它被称为 ProtocolType )是不是从那里你想用 PyObject_IsInstance ,首先声明可见与原型:

You could use the corresponding PyTypeObject if it is defined in the same module. If the type object (let's say it's called ProtocolType) is not visible from where you want to use PyObject_IsInstance, first declare a prototype with:

extern PyTypeObject ProtocolType;

,然后用它是这样的:

And then use it like this:

PyObject_IsInstance(object, reinterpret_cast<PyObject*>(&ProtocolType)

这篇关于如何使用PyObject_IsInstance与非内置类作为第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:41