Python - 将 PyInt 转换为 C int

标签 python c python-c-api

我当前在尝试检索 python 代码返回的值时遇到问题。

我的代码草稿是:

# I call a function that returns a PyObject
PyObject* pyObj_val = call_binding(...);

# This PyObject seems to be a PyInt as PyInt_Check returns 1
assert(PyInt_Check(pyObj_val) == 1);

# Now the only way i found to convert this PyInt to a [C] int is:
int my_val = (int) PyInt_AsLong(pyObj_val);

我想有一种方法可以执行此操作,而无需经过长时间的转换和 int 转换。有人知道吗?

感谢您考虑我的请求。

最佳答案

简单地说:不,这就是解决问题的方法。没有单独的 PyInt_AsInt 函数。

关于Python - 将 PyInt 转换为 C int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53633102/

相关文章:

c++ - PyImport_ImportModule 和 import 语句是否加载到不同的命名空间?

python - 使用 BS4 抓取雅虎财经统计数据的网页

python - Pandas - 从包含列表的字典中创建只有一行的数据框

c - glTranslatef 放置问题

c - 什么时候指针类型之间的转换不是 C 中的未定义行为?

python - 分析回调到 Python 的 C 扩展

python - Flask 配置文件 - 'DEBUG=True' 什么都不做

python - 当模块未记录其所有错误时要捕获哪些错误? (pliSTLib)

c - YUV422 中仅使用 Y 的透明度实现

python - 如何正确使用 Python 的 C API 和异常?