[Python-Dev] __contains__ hook

Guido van Rossum guido@CNRI.Reston.VA.US
Wed, 02 Feb 2000 18:49:34 -0500


> Shouldn't 'in' be a slot of the sequence methods ? I'd suggest
> creating a new tp_flag bit and then extending tp_as_sequence
> with:
> 
> 	binaryfunc sq_contains;
> 
> plus of course add an abstract function to abstract.c:
> 
> 	PySequence_Contain(PyObject *container, PyObject *element)

That function already exists, spelled "PySequence_Contains" (currently
it does the C equivalent of

   for i in container:
       if element == i: return 1
   return 0

I'm not entirely sure whether the 'contains' slot should be part of
the as_sequence struct, but I suppose it makes sense historically.
(The as_number, as_sequece, as_mapping structs don't make sense at all
in the grand scheme of things, but we're stuck with them for the time
being.)

--Guido van Rossum (home page: http://www.python.org/~guido/)