2015-09-27 66 views
-2

我在Python C API中發現了一個奇怪的情況。Python C API:如何評估布爾值的自定義對象?

我有一個自定義的python對象,當它用bool內建函數計算時,他的返回值是False。

如何使自定義對象的類型評估爲True?

Declaration of the object

我注意到這個問題,當我嘗試評估函數(代表一個功能的自定義對象)如下:

if cutomobject: # False! But the object is a valid reference. 

The thread module makes this check, and this prevents me of execute thread

回答

0

像往常一樣,我沒有理解我問題

但是正確的答案在這裏:

c.tp_as_mapping

static PyMappingMethods LuaObject_as_mapping = { 
#if PY_VERSION_HEX >= 0x02050000 
    (lenfunc)LuaObject_length, /*mp_length*/ 
#else 
(inquiry)LuaObject_length, /*mp_length*/ 
#endif 
    (binaryfunc)LuaObject_subscript,/*mp_subscript*/ 
    (objobjargproc)LuaObject_ass_subscript,/*mp_ass_subscript*/ 
}; 

```