使用 Python C API 实现 PyMyType_Check 方法?
所有 Python 提供的类型都有一个检查方法(即 PyList_Check
),允许您检查任意 PyObject*
是否实际上是特定类型。
我如何为我自己的类型实现这个?我在网上没有找到任何好的东西,尽管这似乎是一件很正常的事情。
另外,也许我不擅长浏览大型源代码树,但我一生都无法在 Python (2.5) 源代码中找到 PyList_Check
或其任何同伴的实现。
All the Python-provided types have a check method (i.e., PyList_Check
) that allows you to check if an arbitrary PyObject*
is actually a specific type.
How can I implement this for my own types? I haven't found anything good online for this, though it seems like a pretty normal thing to want to do.
Also, maybe I'm just terrible at looking through large source trees, but I cannot for the life of me find the implementation of PyList_Check
or any of it's companions in the Python (2.5) source.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为它们是宏 使用深层魔法。为自己省点麻烦,使用
PyObject_IsInstance()
代替。
That's because they're macros that use deep magic. Save yourself a bit of headache and use
PyObject_IsInstance()
instead.如果您想实现自己的宏,您可以尝试...
If you'd like to implement your own macro, you could try...