2012-06-11 68 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