2012-06-11 66 views
0

操作我有一類.def INES的__getitem____setitem__方法(和keysitems也一樣),並表現的像字典,其中鍵是字符串。「在」與升壓::蟒蛇

然而,in運營商並不像預期的那樣:

>>> myObject=MyClass() 
>>> 'abc' in myObject.keys() 
False 
>>> 'abc' in myObject 
ArgumentError: Python argument types in 
    MyClass.__getitem__(MyClass, int) 
did not match the C++ signature: 
    __getitem__(MyClass {lvalue}, std::string) 

爲什麼蟒蛇試圖調用__getitem__int,當我用鑰匙str

+0

[documentation](http://docs.python.org/reference/datamodel.html#object.__getitem__)表示「鍵應該是整數和切片對象」。 – unholysampler

+0

您引用的文檔明確指出,這只是需要整數和切片的序列。 – eudoxos

回答

1

似乎

'abc' in myObject 

被評價爲:

for i in myObject: 
    if myObject[i] == 'abc': 
     return true 

i是一個整數。

嘗試執行__contains__(self, value)魔法。

+0

是的,就是這樣!我一直在爲'__in__'尋找文檔而沒有成功。謝謝! – eudoxos