2015-07-09 54 views
0

我將一個python模塊傳遞給C作爲PyObject。我要檢查,看看是否這個值是無我的C代碼,使用這種形式:錯誤:類型'PyObject'(aka'_object')的值不能上下文轉換爲'bool'

int func(PyObject tmp) 
{ 
    if(tmp) 
    { 
    // etc 

我收到以下錯誤。我如何從PyObject轉換爲布爾值,與Python的if函數行爲相似。值得注意的是,當tmpboost::python::object變量時,此命令按預期工作。

ex_program.cpp:72:7: error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool' 
    if (tmp) 
     ^~~ 
+0

NULL或無?他們是非常不同的。 –

+0

無。謝謝你的澄清,我已經編輯了我的備忘錄來反映這一點。 – kilojoules

回答

1

PyObject_IsTrue seems to do what you want

int PyObject_IsTrue(PyObject *o) 

    Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1. 
+0

tmp是否必須是'PyObject *'類型?我用tmp作爲'PyObject'實現這個問題...... – kilojoules

+0

我用'if(PyObject_IsTrue(&tmp))'克服了錯誤' – kilojoules

相關問題