ctypes的类型提示

2024-05-12 20:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以键入提示actype类型吗

from ctypes import *

def wrap_function(library: str, name: str, restype, argtypes) -> *What goes here*:
    """Simplify wrapping ctypes functions"""
    func = library.__getattr__(name)
    func.restype = restype
    func.argtypes = argtypes
    return func

检查我在获取^{之前创建的ctypes函数。我将如何在python中指出这一点

我试过了

ctypes.POINTER(ctypes.CFUNCTYPE)

但它失败了

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: must be a ctypes type

Tags: namefromimport类型键入deflibraryfunction
1条回答
网友
1楼 · 发布于 2024-05-12 20:17:23

CFUNCTYPE是一个函数原型,而不是ctypes类型。 您可以使用CFUNCTYPE定义函数类型,然后将其包装在指针中,例如

from ctypes import *
POINTER(CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int)))

您可以使用定义的类型来输入提示。我看到你在试图“简化”包装。但是,当函数的目的是返回具有动态类型的函数时,不能引入静态类型

相关问题 更多 >