简体   繁体   English

使用扩展在Python中创建新类型

[英]Creating new types in Python using extensions

I've read the documentation and coded up my own example based on their Noddy example: 我已经阅读了文档,并根据他们的Noddy示例编写了自己的示例:

https://docs.python.org/3.4/extending/newtypes.html https://docs.python.org/3.4/extending/newtypes.html

This is a bit of a departure from using Py_BuildValue() to build arbitrary objects dynamically. 这与使用Py_BuildValue()动态构建任意对象有些偏离。 It appears that the underlying assumption is that there's a C structure backing every object instance you want to create. 似乎基本假设是,有一个C结构支持您要创建的每个对象实例。

Is this really the only (best?) way to create new objects in Python? 这真的是在Python中创建新对象的唯一(最佳方法)吗? I was hoping to create a new object and declare members/types similar to how you would create tuples, lists, and dictionaries etc. It seems like it should be possible to declare a new type, declare members and their types, and then plug values in field-by-field w/o all this. 我希望创建一个新对象并声明成员/类型,类似于创建元组,列表和字典等的方式。似乎应该可以声明一个新类型,声明成员及其类型,然后插入值没有逐个字段。

I can't even use the structure defined by the existing API of what I'm gluing because Python objects require the compulsory PyObject_HEAD field. 我什至不能使用现有API定义的结构,因为Python对象需要强制性的PyObject_HEAD字段。 I have to define a new C structure, copy the relevant elements that were handed back to me from the API call. 我必须定义一个新的 C结构,复制从API调用传回给我的相关元素。 It just seems like a level of memory management on the C side that should be unnecessary. 似乎在C端的内存管理级别应该是不必要的。

Sure, there's another way to create types in Python. 当然,还有另一种在Python中创建类型的方法。 Perhaps you've seen it? 也许您已经看过?

class Foo():
    def __init__(self):
        ....

I'm being a little glib, of course. 我当然有点傻瓜。 But when exposing a C interface to Python, it's often a good idea to have a Python script doing much of the interfacing; 但是,当将C接口暴露给Python时,通常最好让Python脚本执行大部分接口。 after all, it's quicker to write Python code than CPython binding code. 毕竟,编写Python代码要比CPython绑定代码更快。 And if what you have is basically an opaque handle type and a set of C functions which create/operate on those handles, you can simply expose those functions as free functions, and use them from your Python-side binding code. 并且,如果您基本上拥有一个不透明的句柄类型和在这些句柄上创建/操作的C函数集,则可以简单地将这些函数公开为自由函数,并从Python端绑定代码中使用它们。 (You might even be able to do it without writing a single line of CPython binding code, using ctypes .) (您甚至可以不用使用ctypes编写一行CPython绑定代码就可以做到这一点。)

So, rather than creating a new type, I was reading up on manipulating tuples in piecewise fashion and there's a section on struct sequences. 因此,我没有创建新的类型,而是阅读了以分段方式操作元组的内容,还有关于struct sequences.的部分struct sequences. This is sort of what I was originally envisioning: 这是我最初设想的:

https://docs.python.org/3.4/c-api/tuple.html https://docs.python.org/3.4/c-api/tuple.html

It doesn't have all the power that a new type provides, but for communicating struct-like objects back to the caller with named attributes, this seems like a pretty easy way to go. 它没有新类型提供的所有功能,但是为了将具有命名属性的类似结构的对象传递回调用者,这似乎是一种非常简单的方法。

I'm curious what you folks think about this... 我很好奇你们对此的看法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM