首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ctypes vs _ctypes -为什么后者存在?

ctypes vs _ctypes -为什么后者存在?
EN

Stack Overflow用户
提问于 2019-10-20 17:37:18
回答 1查看 739关注 0票数 12

我最近了解到,Python不仅有一个名为ctypes的模块(它有一个文档页 ),而且还有一个名为_ctypes的模块,这个模块的不会 (但仍然是在文档中提到过几次)。互联网上的一些代码,比如这个堆栈溢出回答中的代码片段,使用了这个神秘的无文档的_ctypes模块。

一些实验表明,这两个模块具有相似但不相同的docstring和重叠但不相同的属性列表:

代码语言:javascript
复制
Python 3.7.4 (default, Sep  7 2019, 18:27:02) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes, _ctypes
>>> print(ctypes.__doc__)
create and manipulate C data types in Python
>>> print(_ctypes.__doc__)
Create and manipulate C compatible data types in Python.
>>> dir(ctypes)
['ARRAY', 'ArgumentError', 'Array', 'BigEndianStructure', 'CDLL', 'CFUNCTYPE', 'DEFAULT_MODE', 'LibraryLoader', 'LittleEndianStructure', 'POINTER', 'PYFUNCTYPE', 'PyDLL', 'RTLD_GLOBAL', 'RTLD_LOCAL', 'SetPointerType', 'Structure', 'Union', '_CFuncPtr', '_FUNCFLAG_CDECL', '_FUNCFLAG_PYTHONAPI', '_FUNCFLAG_USE_ERRNO', '_FUNCFLAG_USE_LASTERROR', '_Pointer', '_SimpleCData', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_c_functype_cache', '_calcsize', '_cast', '_cast_addr', '_check_size', '_ctypes_version', '_dlopen', '_endian', '_memmove_addr', '_memset_addr', '_os', '_pointer_type_cache', '_reset_cache', '_string_at', '_string_at_addr', '_sys', '_wstring_at', '_wstring_at_addr', 'addressof', 'alignment', 'byref', 'c_bool', 'c_buffer', 'c_byte', 'c_char', 'c_char_p', 'c_double', 'c_float', 'c_int', 'c_int16', 'c_int32', 'c_int64', 'c_int8', 'c_long', 'c_longdouble', 'c_longlong', 'c_short', 'c_size_t', 'c_ssize_t', 'c_ubyte', 'c_uint', 'c_uint16', 'c_uint32', 'c_uint64', 'c_uint8', 'c_ulong', 'c_ulonglong', 'c_ushort', 'c_void_p', 'c_voidp', 'c_wchar', 'c_wchar_p', 'cast', 'cdll', 'create_string_buffer', 'create_unicode_buffer', 'get_errno', 'memmove', 'memset', 'pointer', 'py_object', 'pydll', 'pythonapi', 'resize', 'set_errno', 'sizeof', 'string_at', 'wstring_at']
>>> dir(_ctypes)
['ArgumentError', 'Array', 'CFuncPtr', 'FUNCFLAG_CDECL', 'FUNCFLAG_PYTHONAPI', 'FUNCFLAG_USE_ERRNO', 'FUNCFLAG_USE_LASTERROR', 'POINTER', 'PyObj_FromPtr', 'Py_DECREF', 'Py_INCREF', 'RTLD_GLOBAL', 'RTLD_LOCAL', 'Structure', 'Union', '_Pointer', '_SimpleCData', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_cast_addr', '_memmove_addr', '_memset_addr', '_pointer_type_cache', '_string_at_addr', '_unpickle', '_wstring_at_addr', 'addressof', 'alignment', 'buffer_info', 'byref', 'call_cdeclfunction', 'call_function', 'dlclose', 'dlopen', 'dlsym', 'get_errno', 'pointer', 'resize', 'set_errno', 'sizeof']

我一时以为我看到的可能是一个“加速器模块”,但我认为不可能,因为ctypes无条件地从_ctypes中导入东西。也不清楚_ctypes仅仅是一个实现细节;它至少公开了一个公共成员PyObj_FromPtr,这是有用的,不是通过ctypes模块提供的,也不是CPython源代码中的任何地方使用的--这也许意味着我们在编写ctypes代码时可以导入和使用它?

为什么Python的这两个模块的名称基本相同?两者之间的责任分工是什么,我什么时候会使用其中一种而另一种?我应该把_ctypes看作是标准库的一部分,还是我不应该触及的实现细节?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-20 17:56:14

_ctypes的存在是因为大量的ctypes必须用C编写,ctypes_ctypes都存在,因为并不是所有的ctypes都必须用C编写。ctypes包含更方便用Python编写的部分。

_ctypes中包含没有前导下划线的东西,没有被ctypes公开,这并不意味着这些东西被任何东西所使用。有时候程序只剩下这样的东西。在发展的某个时候,可能会有公开它的意图。

_ctypes是一个实现细节。使用ctypes

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58475560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档