gpt4 book ai didi

python - Py_BuildValue : make tuple with bool?

转载 作者:太空狗 更新时间:2023-10-29 20:12:35 27 4
gpt4 key购买 nike

我在 docs 中看到,我可以用 int 构建元组值(指定“i”)。我需要用 bool 制作元组,例如(真,10)。我怎样才能用 bool 制作这样的元组(需要什么说明符)?

最佳答案

该转换没有预定义的格式字符,但通过将 Py_TruePy_False 对象插入到元组中来模拟一个是微不足道的。例如:

int i = ...;
bool b = ...;
PyObject *tuple_with_bool = Py_BuildValue("Oi", b ? Py_True: Py_False, i);

另一种选择是使用 PyBool_FromLong做转换。在这种情况下,请记住使用 N 格式来说明 PyBool_FromLong 返回新引用:

PyObject *tuple_with_bool = Py_BuildValue("Ni", PyBool_FromLong(b), i);

关于python - Py_BuildValue : make tuple with bool?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com