0

I have a class which .defines the __getitem__, __setitem__ methods (and keys and items as well), and behaves like a dictionary, where keys are strings.

However, the in operator does not behave as expected:

>>> 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)

Why is python trying to call __getitem__ with int, when I use the str key?

eudoxos
  • 18,545
  • 10
  • 61
  • 110
  • The [documentation](http://docs.python.org/reference/datamodel.html#object.__getitem__) says that "keys should be integers and slice objects". – unholysampler Jun 11 '12 at 11:27
  • The doc you reference mentions explicitly that is only sequences which want integers and slices. – eudoxos Jun 11 '12 at 12:13

1 Answers1

1

It seems that

'abc' in myObject 

is being evaluated as:

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

Where i is an integer.

Try implementing the __contains__(self, value) magic method.

Eric
  • 95,302
  • 53
  • 242
  • 374